AnimatorFinishEventTrigger.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Animator finish event trigger.
/// </summary>
public class AnimatorFinishEventTrigger : MonoBehaviour
{
/// <summary>
/// Occurs when on finish animation.
/// </summary>
public event Action<string> OnFinishAnimation;
/// <summary>
/// Awake this instance.
/// </summary>
void Awake()
{
// Get a reference to the Animator Controller:
RuntimeAnimatorController ac = GetComponent<Animator>().runtimeAnimatorController;
string methodName = (OnFinishAnimationTrigger as Action<string>).Method.Name;
foreach(var clip in ac.animationClips)
{
var finishEvent = new AnimationEvent();
finishEvent.functionName = methodName;
finishEvent.stringParameter = clip.name;
finishEvent.time = clip.length;
clip.AddEvent(finishEvent);
}
}
/// <summary>
/// Raises the finish animation trigger event.
/// </summary>
/// <param name="name">Name.</param>
private void OnFinishAnimationTrigger(string name)
{
if(this.OnFinishAnimation != null)
{
this.OnFinishAnimation(name);
}
}
}
このAnimatorFinishEventTrigger
コンポーネントを任意のゲームオブジェクトへ貼る。
これをGetComponent
等で取得して、
GetComponent<AnimatorFinishEventTrigger>().OnFinishAnimation = (str)=>{Debug.Log(str)};
のようにする。