0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AnimationClip再生終了後に呼ばれるイベントを設定するスクリプト

Posted at

ちょっとした動作確認の時などに用意してあるとちゃちゃっと設定できて便利なのですが、
いざ実装してみようとした所ちゃちゃっと終わらなかったので、備忘録として残しておきます。

AnimationFinishedEventTrigger.cs

using UnityEngine;
using UnityEngine.Events;

[RequireComponent(typeof(Animation))]
public class AnimationFinishedEventTrigger : MonoBehaviour
{
    [SerializeField] private UnityEvent onFinished = null;
    Animation selfAnimation;

    private void Start()
    {
        selfAnimation = GetComponent<Animation>();

        var animationEvent = new AnimationEvent
        {
            functionName = "OnFinished",
            time = selfAnimation.clip.length
        };
        selfAnimation.clip.AddEvent(animationEvent);
    }

    public void OnFinished()
    {
        if (onFinished != null) onFinished.Invoke();
    }
}

使い方

上記のコンポーネントをAnimationが付いているGameObjectに追加して、
呼び出したいメソッドをインスペクタから設定するだけです。
※uGUIのButton等と同じ要領
キャプチャ.JPG

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?