Unity—动画事件传递
2022-02-22 10:56
579 查看
动画机在子节点的预制体上,动画事件函数需要在Animator组件同一个GameObject上;
写起来会比较麻烦,用事件将动画事件传递到父物体的脚本中;
public class AnimaEventDelivery : MonoBehaviour { [Header("指定传递动画事件")] public GameObject animaGO; [Header("向上传递事件")] public bool isParentDelivery = true; //事件 public void OnAnimaEvent(string args) { if (isParentDelivery) { var receive = gameObject.GetComponentsInParent<IAnimaEvent>(); if (receive != null) { for (int i = 0; i < receive.Length; i++) { receive[i].OnAnimaEvent(args); } } } else if (animaGO != null) { var e = animaGO.GetComponent<IAnimaEvent>(); if (e == null) Debug.LogError("Monohavior not implement IAnimaEvent"); else e.OnAnimaEvent(args); } } } //普通传递事件,接口 public interface IAnimaEvent { void OnAnimaEvent(string args); }
在父节点中继承IAnimaEvent接口,实现方法;
拦截string参数,响应不同事件;
public class Hero :IAnimaEvent { //动画事件传递 public void OnAnimaEvent(string args) { if (args == "HitEnd") { Debug.log("HitEnd"); } else if (args == "Fire") { Debug.log("Fire"); } else if (args == "Die") { Debug.log("Die"); } } }
perfab节点挂AnimaEventDelivery脚本;
添加动画事件,设置不同string参数;
相关文章推荐
- Unity动画播放结束事件
- Unity/Animation -- 添加动画事件(Animation Events)
- Unity动画事件
- Unity动画事件
- Unity游戏开发——自动为动画剪辑添加事件 之 最后几帧的事件不能被调用的问题
- Unity之NGUIUIButton点击事件的带参传递
- Unity (二) Animator:Events - 动画事件案例(呐喊出现字幕)
- Unity动画系统 动态添加动画事件
- unity中处理animator动画事件
- unity 自己写的动画事件工具类
- Unity动画帧添加事件
- 【Unity】 旧版动画 在某一帧增添事件
- Unity 脚本之间的消息传递,事件管理
- Unity动画事件
- Unity_DOTween动画的学习(十一)_DOTweenPath路径编辑器的使用补充_结合按钮触发不同点击事件控制播放状态_Waypoints
- Unity (二) Animator:Events : 动画事件案例(呐喊出现字幕)
- Unity动画事件
- Unity之一天一个技术点(十五)---给unity3d的老版动画中,添加事件的方法
- Unity 动画结束事件
- unity5 动画系统Mecanim事件快速入门 区分animation和animator不再混淆