Unity3D|Unity 动态加载Animator Event 事件

【Unity3D|Unity 动态加载Animator Event 事件】前一阵子频繁更改模型,总是手动添加动画事件 animator events一直比较麻烦,我就想能不能动态的生成对应的动画事件呢,然后找了一些资料,试了下感觉还不错,分享给大家,欢迎大家留言交流~需要注意的是修改后的动画事件重新动态加载后依然保留,猜测注册修改的是元数据,除非注销或者程序停止才能恢复。

using UnityEngine; using System.Collections; public class DynamicLoading_AninatorEvents_SZQ : MonoBehaviour {private Animator m_Animator = null; private RuntimeAnimatorController m_runtimeAnimatorController = null; private AnimationClip[] clips = null; void Start() { m_Animator = GetComponent(); // 获取运行时运行时动画器控制器 m_runtimeAnimatorController = m_Animator.runtimeAnimatorController; //获取含有的动画片段 clips = m_runtimeAnimatorController.animationClips; //根据动画名称设置对应的事件 for (int i = 0; i < clips.Length; i++) { if (clips[i].events.Length == 0) switch (clips[i].name) { case "animator_0": { AnimationEvent m_animator_0_Start = new AnimationEvent(); AnimationEvent m_animator_0_Running = new AnimationEvent(); AnimationEvent m_animator_0_End = new AnimationEvent(); //对应事件触发相应函数的名称 m_animator_0_Start.functionName = "Animator_0_Start_Event"; m_animator_0_Running.functionName = "Animator_0_Running_Event"; m_animator_0_End.functionName = "Animator_0_End_Event"; //设定对应事件在相应动画时间轴上的触发时间点 m_animator_0_Start.time = 0; //对应动画开始处触发 m_animator_0_Running.time = clips[i].length * 0.5f; //中间 m_animator_0_End.time = clips[i].length; //结尾//把事件添加到时间轴上 clips[i].AddEvent(m_animator_0_Start); clips[i].AddEvent(m_animator_0_Running); clips[i].AddEvent(m_animator_0_End); } break; case "animator_1": { AnimationEvent m_animator_1_Start = new AnimationEvent(); AnimationEvent m_animator_1_Running = new AnimationEvent(); AnimationEvent m_animator_1_End = new AnimationEvent(); //对应事件触发相应函数的名称 m_animator_1_Start.functionName = "Animator_1_Start_Event"; m_animator_1_Running.functionName = "Animator_1_Running_Event"; m_animator_1_End.functionName = "Animator_1_End_Event"; //设定对应事件在相应动画时间轴上的触发时间点 m_animator_1_Start.time = 0; //对应动画开始处触发 m_animator_1_Running.time = clips[i].length * 0.5f; //中间 m_animator_1_End.time = clips[i].length; //结尾//把事件添加到时间轴上 clips[i].AddEvent(m_animator_1_Start); clips[i].AddEvent(m_animator_1_Running); clips[i].AddEvent(m_animator_1_End); } break; } } //重新绑定动画器的所有动画的属性和网格数据。 m_Animator.Rebind(); } void Animator_0_Start_Event() { } void Animator_0_Running_Event() { } void Animator_0_End_Event() { }void Animator_1_Start_Event() { } void Animator_1_Running_Event() { } void Animator_1_End_Event() { }/// /// 注销对应事件 /// void UnSubscription() { for (int i = 0; i < clips.Length; i++) { clips[i].events = default(AnimationEvent[]); } } }



Unity3D|Unity 动态加载Animator Event 事件
文章图片
Unity3D|Unity 动态加载Animator Event 事件
文章图片
Unity3D|Unity 动态加载Animator Event 事件
文章图片







    推荐阅读