コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Animator を使用せずに 2D スプライトアニメーションを実装できる「RetroSpriteAnimator」紹介

はじめに

「RetroSpriteAnimator」を Unity プロジェクトに導入することで
Animator を使用せずに 2D スプライトアニメーションを実装できるようになります

使用例

f:id:baba_s:20181231162431g:plain

使い方

f:id:baba_s:20181231162426p:plain

あらかじめアニメーションさせたいスプライトを Unity プロジェクトに追加して
Sprite Mode を「Multiple」にして用意しておきます

f:id:baba_s:20181231162533p:plain

シーンにスプライトのオブジェクトを作成して
「RetroSpriteAnimator」をアタッチして
「Sprite」にアニメーションさせたいスプライトを設定します

using UnityEngine;

public class Example : MonoBehaviour
{
    // Inspector で参照を設定
    public RetroSpriteAnimator m_animator;

    private void Awake()
    {
        // アニメーションを追加
        m_animator.AddAnimation
        (
            Name        : "idle",
            Frames      : new int[] { 0, 1, 2, 1 },
            FrameRate   : 5
        );

        // 追加したアニメーションを再生
        m_animator.PlayAnimation( "idle" );
    }
}

そして上記のようなコードを記述することで使用できます