Edited at

UnityのAnimationのAnimationClipの末尾全部にFinishEventをぶっさすスクリプト

More than 3 years have passed since last update.

使い方

・Animationの刺さっているObjectにこいつをAddComponent

・AnimationのFinishイベントを拾いたいコンポーネントはこいつのOnFinishAnimationにイベントを追加

・特定のClipがアニメーション終了した時にそのClipの名前がイベントに飛んでくる。


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

[RequireComponent (typeof(Animation))]
public class AnimationFinishEventTrigger : MonoBehaviour
{
public event Action<string> OnFinishAnimation;

void Awake()
{
var animation = this.GetComponent<Animation> ();
foreach (AnimationState state in animation) {
var finishEvent = new AnimationEvent();
finishEvent.functionName = "OnFinishAnimationTrigger";
finishEvent.stringParameter = state.clip.name;
finishEvent.time = state.clip.length;
state.clip.AddEvent(finishEvent);
}
}

void OnFinishAnimationTrigger(string name)
{
Debug.Log ("--"+this.gameObject.name+"-OnFinishAnimation: " + name );
if (OnFinishAnimation != null)
OnFinishAnimation (name);
}
}