private static void OnListenerRemoving(GameEventType eventType, Delegate callBack) { if (eventDic.ContainsKey(eventType))//判断存在所要移除键时,所存在的错误情况,并抛出错误 { Delegate d = eventDic[eventType]; if (d == null) { throw new Exception(String.Format("移除错误:移除委托为空,移除委托类型为{1}", eventType.GetType())); } else if (d.GetType() != callBack.GetType()) { throw new Exception(String.Format("移除错误:移除委托类型不匹配,所要移除委托类型为{1}", eventType.GetType())); } //eventDic.Remove(eventType); } else {//若所要移除的键,不存在直接返回错误 throw new Exception(String.Format("移除错误:不存在所要移除的键,所要移除的键名为{1}", eventType.GetType())); } }
//广播5个传参的事件 public static void Boardcast <T, U, V, W, X>(GameEventType eventType, T arg0, U arg1, V arg2, W arg3, X arg4) { Delegate d; if (eventDic.TryGetValue(eventType, out d)) { CallBack <T, U, V, W, X> callBack = d as CallBack <T, U, V, W, X>; if (callBack != null) { callBack(arg0, arg1, arg2, arg3, arg4); } else { throw new Exception(String.Format("不存在对应的委托值,所要广播的委托类型为{1}", eventType.GetType())); } } }
//广播无参事件 public static void Boardcast(GameEventType eventType) { Delegate d; if (eventDic.TryGetValue(eventType, out d)) { CallBack callBack = d as CallBack; if (callBack != null) { callBack(); } else { throw new Exception(String.Format("不存在对应的委托值,所要广播的委托类型为{1}", eventType.GetType())); } } }
//广播无参事件 public static T Boardcast <T>(GameEventType eventType) { Delegate d; if (eventDic.TryGetValue(eventType, out d)) { CallBack_Return <T> callBack = d as CallBack_Return <T>; if (callBack != null) { return(callBack()); } else { throw new Exception(String.Format("不存在对应的委托值,所要广播的委托类型为{1}", eventType.GetType())); } } return(default(T)); }
private static void OnListenerAdding(GameEventType eventType, Delegate callBack) { if (!eventDic.ContainsKey(eventType)) { eventDic.Add(eventType, null); } Delegate d = eventDic[eventType]; if (d != null && d.GetType() != callBack.GetType()) { throw new Exception(String.Format("尝试为事件添加不同类型的委托,当前事件所对应的委托为{1},要添加的委托类型为{2}", eventType.GetType(), callBack.GetType())); } }