Unity code adds Animator animation callbacks

Manually add animation callback events

In the upper right frame of the animation, adding Animation Event;
selecting the method of adding Inspector.
Mounted on the respective need the script has Animator object, before selecting its public methods.
Here Insert Picture Description

Add code animation callbacks

Need to use the class
Animator,
AnimatorOverrideController for covering animation clip controllers to head for a given specialized animation, does not reset the current state of the state machine,
AnimationClip storage keyframe-based animation,
AnimationEvent animation events,
Action.
Parameter definitions use the Dictionary storage AnimationClip

Add animation event code:
Method One: Clip.AddEvent (); valid until the end of the run.
Method two: AnimationUtility.SetAnimationEvents (), equivalent to manually add permanent.

  	private Animator mAnim;
    private AnimatorOverrideController mController;
    private Dictionary<string, AnimationClip> mClipDic = new Dictionary<string, AnimationClip>();
    private Action mCallBack;
    private AnimationEvent evt;

initialization

  public void Awake()
    {
        mController = new AnimatorOverrideController();
        mAnim = transform.GetComponent<Animator>();
        //通过Animator中的runtimeAnimatorController,将AnimatorOverrideController与Animator链接
        mController.runtimeAnimatorController = mAnim.runtimeAnimatorController;
    }

Play calling

public void Play(string animName, Action action)
    {
    	//回调事件
        mCallBack = action;
        //避免重复添加多个回调函数到动画中。
        if (!mClipDic.ContainsKey(animName))
        {
        	//通过AnimatorOverrideController[动画名],获取clip
            mClipDic.Add(animName, mController[animName]); 
            //初始化AnimationEvent 动画事件。
            evt = new AnimationEvent();
            //事件挂载的时间位置。
            evt.time = mClipDic[animName].length;
            //事件调用的函数名
            evt.functionName = "MyCallback";
            //加入到clip中。有效期:播放结束
            //mClipDic[animName].AddEvent(evt);
            //加入到clip中。有效期:永久。直到手动删除。
            AnimationUtility.SetAnimationEvents(mClipDic[animName], new AnimationEvent[] { evt });
        }
        //播放动画
        mAnim.Play(animName,0,0);
    }
    
    private void MyCallback()
    {
        if(mCallBack != null)
            mCallBack.Invoke();
    }

The complete code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NaughtyAttributes;
using UnityEngine.Events;
using UnityEditor;
using System;
/// <summary>
/// 播放Animator并加入播放完成回调。
/// </summary>
[RequireComponent(typeof(Animator))]
public class UIAnim : MonoBehaviour
{
    private Animator mAnim;
    private AnimatorOverrideController mController;
    private Dictionary<string, AnimationClip> mClipDic = new Dictionary<string, AnimationClip>();
    private Action mCallBack;
    private AnimationEvent evt;
    public void Awake()
    {
        mController = new AnimatorOverrideController();
        mAnim = transform.GetComponent<Animator>();
        mController.runtimeAnimatorController = mAnim.runtimeAnimatorController;
    }

    public void Play(string animName, Action action)
    {
        mCallBack = action;
        if (!mClipDic.ContainsKey(animName))
        {
            mClipDic.Add(animName, mController[animName]); 
            evt = new AnimationEvent();
            evt.time = mClipDic[animName].length;
            evt.functionName = "MyCallback";
            //mClipDic[animName].AddEvent(evt);
            AnimationUtility.SetAnimationEvents(mClipDic[animName], new AnimationEvent[] { evt });

        }
        mAnim.Play(animName,0,0);

    }
    private void MyCallback()
    {
        if(mCallBack != null)
            mCallBack.Invoke();
    }
}

Released seven original articles · won praise 1 · views 177

Guess you like

Origin blog.csdn.net/boyZhenGui/article/details/104016087