Lua监听AnimationEvent
2017-05-11 00:00
561 查看
using System; using UnityEngine; //using LuaInterface; //using UnityEngine.Events; using System.Collections.Generic; namespace LuaFramework { [RequireComponent(typeof(Animator))] public class AnimEventListener : MonoBehaviour { private Animator animator; private Dictionary<string, AnimEvent> eventsMap; public delegate void ObjectDelegate(object arg0); public class AnimEvent { public object arg0; private event ObjectDelegate m_animEvent; public AnimEvent(object arg0, ObjectDelegate callback) { this.arg0 = arg0; m_animEvent += callback; } public void Invoke() { Invoke(arg0); } public void Invoke(object arg) { if (m_animEvent != null) { m_animEvent(arg); } } public void AddListener(ObjectDelegate callback) { m_animEvent += callback; } public void RemoveListener(ObjectDelegate callback) { m_animEvent -= callback; } public void RemoveAllListeners() { m_animEvent = null; } } public static AnimEventListener Get(GameObject go) { if (go == null) return null; AnimEventListener listener = go.GetComponent<AnimEventListener>(); if (listener == null) listener = go.AddComponent<AnimEventListener>(); return listener; } public bool AddEvent(string animName, float time, ObjectDelegate callback) { AnimationClip[] clips = animator.runtimeAnimatorController.animationClips; AnimationClip target = null; for (int i = 0; i < clips.Length; i++) { if (animName == clips[i].name) { target = clips[i]; break; } } if (target == null) return false; AnimEvent eventParam = null; string animEventName = string.Format("{0}_{1}", animName, time.ToString()); if (!eventsMap.TryGetValue(animEventName, out eventParam)) { AnimationEvent animEvent = new AnimationEvent(); animEvent.time = time; animEvent.functionName = "OnAnimEvent"; animEvent.stringParameter = animEventName; eventParam = new AnimEvent(animEvent, callback); eventsMap[animEventName] = eventParam; target.AddEvent(animEvent); } else { eventParam.AddListener(callback); } return true; } public void RemoveEvent() { } void Awake() { animator = GetComponent<Animator>(); eventsMap = new Dictionary<string, AnimEvent>(); } public void OnAnimEvent(AnimationEvent animEvent) { if (animEvent == null) return; AnimEvent eventParam = null; if (eventsMap.TryGetValue(animEvent.stringParameter, out eventParam)) { eventParam.Invoke(); } } } }
相关文章推荐
- cocos2d-x3.2 lua 返回键监听
- 使用Cocos2d-lua开发植物大战僵尸07-实现开始游戏按钮功能和主场景触摸监听
- lua监听内存释放
- cocos2dx-lua sprite增加touch监听
- cocos2d-x lua 监听editBox
- Lua模仿类似观察者模式的监听变量改变自动刷新
- 关于lua中事件监听,多次请求网络的问题
- cocos2dx lua 使用cocostudio编辑的动画文件并监听帧事件
- 项目后期Lua接入笔记06--按钮事件监听及消息分发
- 基于Lua插件化的Pcap流量监听代理
- cocos2dx3.13 lua注册、分发监听真机切到后台事件
- Cocos2dx+lua中调用按键监听
- java 定时器 timer 时间监听 初学
- UITextView中return key点击事件的监听方法
- 监听input text中内容发生变化时触发事件
- cocos2dx-lua 加载和绑定ccbi 中对象
- Android开发监听判断网络连接状态的方法
- ie11下alt+d不能用js监听问题
- Lua HelloWorld
- JS奇技之利用scroll来监听resize详解