当需要调用方法时,将该方法做成一个监听,AddListener(事件码,方法)委托需要与事件码绑定。方法若持有参数,在AddListener前指定同类型的泛型。
传统的方式是通过获取存在方法的类来调取或者采用单例模式实现,但是这样呢代码耦合性高,不易维护,本片博客总结实现事件监听机制完成方法调用。(跟消息发送机制类似)
首先,创建EventType.cs定义事件类型
public enum EventType { }
其次,创建CallBack.cs定义回调方法的委托类型
public delegate void CallBack(); public delegate void CallBack<T>(T arg); public delegate void CallBack<T, X>(T arg1, X arg2); public delegate void CallBack<T, X, Y>(T arg1, X arg2, Y arg3);
最后,创建Event.cs实现方法监听与移除监听(最重要的一环)
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Event { private static Dictionary<EventType, Delegate> _EventTable = new Dictionary<EventType, Delegate>(); #region private static void OnListenerAdding(EventType eventType, Delegate callBack) { if (!_EventTable.ContainsKey(eventType)) { _EventTable.Add(eventType, null); } Delegate d = _EventTable[eventType]; if (d != null && d.GetType() != callBack.GetType()) { throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托是{1},要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType())); } } private static void OnListenerRemoving(EventType eventType, Delegate callBack) { if (_EventTable.ContainsKey(eventType)) { Delegate d = _EventTable[eventType]; if (d == null) { throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType)); } else if (d.GetType() != callBack.GetType()) { throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前委托类型为{1},要移除的委托类型为{2}", eventType, d.GetType(), callBack.GetType())); } } else { throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType)); } } private static void OnListenerRemoved(EventType eventType) { if (_EventTable[eventType] == null) { _EventTable.Remove(eventType); } } #endregion #region 添加监听 //no parameters public static void AddListener(EventType eventType, CallBack callBack) { OnListenerAdding(eventType, callBack); _EventTable[eventType] = (CallBack)_EventTable[eventType] + callBack; } //Single parameters public static void AddListener<T>(EventType eventType, CallBack<T> callBack) { OnListenerAdding(eventType, callBack); _EventTable[eventType] = (CallBack<T>)_EventTable[eventType] + callBack; } //two parameters public static void AddListener<T, X>(EventType eventType, CallBack<T, X> callBack) { OnListenerAdding(eventType, callBack); _EventTable[eventType] = (CallBack<T, X>)_EventTable[eventType] + callBack; } //three parameters public static void AddListener<T, X, Y>(EventType eventType, CallBack<T, X, Y> callBack) { OnListenerAdding(eventType, callBack); _EventTable[eventType] = (CallBack<T, X, Y>)_EventTable[eventType] + callBack; } #endregion #region 移除监听 //no parameters public static void RemoveListener(EventType eventType, CallBack callBack) { OnListenerRemoving(eventType, callBack); _EventTable[eventType] = (CallBack)_EventTable[eventType] - callBack; OnListenerRemoved(eventType); } //single parameters public static void RemoveListener<T>(EventType eventType, CallBack<T> callBack) { OnListenerRemoving(eventType, callBack); _EventTable[eventType] = (CallBack<T>)_EventTable[eventType] - callBack; OnListenerRemoved(eventType); } //two parameters public static void RemoveListener<T, X>(EventType eventType, CallBack<T, X> callBack) { OnListenerRemoving(eventType, callBack); _EventTable[eventType] = (CallBack<T, X>)_EventTable[eventType] - callBack; OnListenerRemoved(eventType); } //three parameters public static void RemoveListener<T, X, Y>(EventType eventType, CallBack<T, X, Y> callBack) { OnListenerRemoving(eventType, callBack); _EventTable[eventType] = (CallBack<T, X, Y>)_EventTable[eventType] - callBack; OnListenerRemoved(eventType); } #endregion #region 播报(执行) //no parameters public static void Broadcast(EventType eventType) { Delegate d; if (_EventTable.TryGetValue(eventType, out d)) { CallBack callBack = d as CallBack; if (callBack != null) { callBack(); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType)); } } } //single parameters public static void Broadcast<T>(EventType eventType, T arg) { Delegate d; if (_EventTable.TryGetValue(eventType, out d)) { CallBack<T> callBack = d as CallBack<T>; if (callBack != null) { callBack(arg); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType)); } } } //two parameters public static void Broadcast<T, X>(EventType eventType, T arg1, X arg2) { Delegate d; if (_EventTable.TryGetValue(eventType, out d)) { CallBack<T, X> callBack = d as CallBack<T, X>; if (callBack != null) { callBack(arg1, arg2); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType)); } } } //three parameters public static void Broadcast<T, X, Y>(EventType eventType, T arg1, X arg2, Y arg3) { Delegate d; if (_EventTable.TryGetValue(eventType, out d)) { CallBack<T, X, Y> callBack = d as CallBack<T, X, Y>; if (callBack != null) { callBack(arg1, arg2, arg3); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType)); } } } #endregion }
写在最后,如果监听方法有多个参数(我只扩展了1,2,3个参数),可按照上面代码方式进行扩展