AnimatorFinishEventTrigger

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);
        }
    }
}
GetComponent<AnimatorFinishEventTrigger>().OnFinishAnimation = (str)=>{Debug.Log(str)};

AnimationEvent的函数名必须是animatior该gameobject上的函数

RuntimeAnimatorController.animationClips是动画片段。clip.name是动画片段名

Animator.play(“xxx”)这里是状态名

发表评论