【Unity】オブジェクトのアニメーションを簡単に実装する

2Dのゲームでオブジェクトをアニメーションさせる場合、プログラムを書くかAnimatorを用意するかになります。
オブジェクトごとにプログラムやAnimatorを用意するのは非常に面倒なので、使いまわせるスクリプトを用意しました。

以下の通り設定すると、このような2Dパタパタアニメが作成出来ます。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ObjectAnimation : MonoBehaviour
{
    public Sprite[] AnimationSprite; //inspectorで動かしたい画像をセットする

    public Image images; //inspectorでImageComponentをセットする
    public float AnimationSpeed; //アニメーション速度を設定 0.1~0.5くらいで調整

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine("Animation"); //アニメーション開始
    }

    IEnumerator Animation(){ 
        while(true){ //アニメーションし続ける
            for(int i =0;i<AnimationSprite.Length;i++){
                images.sprite = AnimationSprite[i];
                yield return new WaitForSeconds(AnimationSpeed);
            }
        }
    }

}

ObjectAnimation.csを作成して、上記のコードを貼り付ける
アニメーションさせたいオブジェクトのinspectorにObjectAnimation.csをドラッグ&ドロップ
AnimationSpriteに用意したアニメーション用の画像を追加していく
AnimationSpeedにアニメーション速度を設定 0.1~0.5辺りで設定してみる

このようになれば完成です。
「ワンダーラン」のようなレトロ風ドット絵のゲームであればAnimationSpeedを0.3以上に、
今風のぬるぬる動く2Dゲームであれば0.1に設定する等、工夫して使っていただければと。

ワンダーラン iOS / Android

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です