public void RegisterHandler(MessageProcessUnitBase module, MessageHandlerDelegate handler, Type msgType) { if (!typeof(IMessage).IsAssignableFrom(msgType)) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("消息结构体[{0}]没有实现IMessage接口", msgType.Name)); } return; } #endif #if UNITY_EDITOR if (!msgType.IsValueType) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("消息结构体[{0}]必须定义为值类型(struct)", msgType.Name)); } return; } object[] fieldsDefined = msgType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (fieldsDefined.Length <= 0) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("消息结构体[{0}]中没有定义任何成员,如果不需要向外通知任何参数的话,使用Notification", msgType.Name)); } return; } #endif if (module != null && handler != null) { MessageHandler messageHandler = new MessageHandler(msgType, handler); if (!messageHandlerTableLocked) { if (messageHandlerTable == null) { messageHandlerTable = new Dictionary <Type, List <MessageHandler> >(); } CollectionUtil.AddIntoTable(msgType, messageHandler, messageHandlerTable); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[本地消息中心]1.注册消息处理器:来自[{0}] 消息类型[{1}] 处理回调[{2}]", module.GetType().Name, msgType.Name, StringUtility.ToString(handler))); } #endif } else { RegisterModifier modifier = pool.Aquire(); modifier.isUnregister = false; modifier.messageType = msgType; modifier.messageHandler = messageHandler; modifier.module = module; if (messageHandlerRegisterChangeCache == null) { messageHandlerRegisterChangeCache = new List <RegisterModifier>(); } messageHandlerRegisterChangeCache.Add(modifier); } if (moduleMessageRegisterTable == null) { moduleMessageRegisterTable = new Dictionary <MessageProcessUnitBase, Dictionary <Type, MessageHandler> >(); } CollectionUtil.AddIntoTable(module, msgType, messageHandler, moduleMessageRegisterTable); } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("注册消息处理器时传递的参数有[null]值:消息类型[{0}] 模块[{1}] 处理回调[{2}]", msgType.Name, StringUtility.ToString(module), StringUtility.ToString(handler))); } } #endif }
public void RegisterHandler(MessageProcessUnitBase module, int noticeID, NotificationHandlerDelegate handler) { if (module != null && handler != null) { NotificationHandler notificationHandler = new NotificationHandler(noticeID, handler); CollectionUtil.AddIntoTable(noticeID, notificationHandler, notificationHandlerTable); CollectionUtil.AddIntoTable(module, noticeID, notificationHandler, moduleNotificationRegisterTable); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[本地通知中心]1.注册通知处理器:来自[{0}] 通知ID[{1}] 处理回调[{2}]", module.GetType().Name, noticeID, StringUtility.ToString(handler))); } #endif } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("注册通知处理器时传递的参数有[null]值:通知ID[{0}] 模块[{1}] 处理回调[{2}]", noticeID, StringUtility.ToString(module), StringUtility.ToString(handler))); } } #endif }
public void UnregisterAllHandlersBelongToModule(MessageProcessUnitBase module) { if (module != null) { if (moduleMessageRegisterTable != null) { Dictionary <Type, MessageHandler> handlerTable; if (moduleMessageRegisterTable.TryGetValue(module, out handlerTable)) { if (messageHandlerTable != null) { foreach (KeyValuePair <Type, MessageHandler> handlerKV in handlerTable) { if (!messageHandlerTableLocked) { CollectionUtil.RemoveFromTable(handlerKV.Key, handlerKV.Value, messageHandlerTable); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[本地消息中心]2.注销消息处理器:来自[{0}] 消息类型[{1}] 处理回调[{2}]", module.GetType().Name, handlerKV.Key.Name, StringUtility.ToString(handlerKV.Value.Handler))); } #endif } else { RegisterModifier modifier = pool.Aquire(); modifier.isUnregister = true; modifier.messageType = handlerKV.Key; modifier.messageHandler = handlerKV.Value; modifier.module = module; if (messageHandlerRegisterChangeCache == null) { messageHandlerRegisterChangeCache = new List <RegisterModifier>(); } messageHandlerRegisterChangeCache.Add(modifier); } } } moduleMessageRegisterTable.Remove(module); } } } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("注销消息处理器时传递的参数有[null]值:模块[{0}]", StringUtility.ToString(module))); } } #endif }
public void UnregisterAllHandlersBelongToModule(MessageProcessUnitBase module) { if (module != null) { Dictionary <int, NotificationHandler> handlerTable; if (moduleNotificationRegisterTable.TryGetValue(module, out handlerTable)) { foreach (NotificationHandler handler in handlerTable.Values) { List <NotificationHandler> tmpList; if (notificationHandlerTable.TryGetValue(handler.noticeID, out tmpList)) { tmpList.Remove(handler); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[本地通知中心]2.注销通知处理器:来自[{0}] 通知ID[{1}] 处理回调[{2}]", module.GetType().Name, handler.noticeID, StringUtility.ToString(handler.Handler))); } #endif } } moduleNotificationRegisterTable.Remove(module); } } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("注销通知处理器时传递的参数有[null]值:模块[{0}]", StringUtility.ToString(module))); } } #endif }