/// <summary> /// 注册消息, /// 注意第一个参数,使用了C# this的扩展, /// 所以只有实现IMsgReceiver的对象才能调用此方法 /// </summary> public static void RegisterLogicMsg(this IMsgReceiver self, string msgName, VoidDelegate.WithParams callback) { if (string.IsNullOrEmpty(msgName)) { Debug.Log("消息系统 【注册】:" + msgName + " is Null or Empty"); return; } if (null == callback) { Debug.Log("消息系统 【注册】:" + msgName + " callback is Null"); return; } if (!mMsgHandlerDict.ContainsKey(msgName)) { mMsgHandlerDict [msgName] = new List <LogicMsgHandler>(); } var handlers = mMsgHandlerDict [msgName]; // 防止重复注册 foreach (var handler in handlers) { if (handler.receiver == self && handler.callback == callback) { Debug.Log("消息系统 【注册】:" + msgName + " 已经注册!"); return; } } handlers.Add(new LogicMsgHandler(self, callback)); }
static bool CheckDelegateNull(VoidDelegate.WithParams callback) { if (null == callback) { Debug.LogWarning("callback is Null"); return(true); } return(false); }
/// <summary> /// 注册消息, /// 注意第一个参数,使用了C# this的扩展, /// 所以只有实现IMsgReceiver的对象才能调用此方法 /// </summary> public static void RegisterGlobalMsg(this IMsgReceiver self, string msgName, VoidDelegate.WithParams callback) { // 略过 if (string.IsNullOrEmpty(msgName)) { Debug.LogError("RegisterMsg:" + msgName + " is Null or Empty"); return; } // 略过 if (null == callback) { Debug.LogError("RegisterMsg:" + msgName + " callback is Null"); return; } // 添加消息通道 if (!mMsgHandlerDict.ContainsKey(TMsgChannel.Global)) { mMsgHandlerDict[TMsgChannel.Global] = new Dictionary <string, List <TMsgHandler> >(); } // 略过 if (!mMsgHandlerDict[TMsgChannel.Global].ContainsKey(msgName)) { mMsgHandlerDict[TMsgChannel.Global][msgName] = new List <TMsgHandler>(); } // 看下这里 var handlers = mMsgHandlerDict[TMsgChannel.Global][msgName]; // 略过 // 防止重复注册 foreach (var handler in handlers) { if (handler.receiver == self && handler.callback == callback) { Debug.LogWarning("RegisterMsg:" + msgName + " ayready Register"); return; } } // 再看下这里 handlers.Add(new TMsgHandler(self, callback)); }
//注册消息 /// <summary> /// 注册消息 -- 默认模式,只注册到全局类型 TMsgChannel.Global /// </summary> /// <param name="msgName">消息名称</param> /// <param name="callback">回调</param> protected void GoRegisterGlobalMsg(string msgName, VoidDelegate.WithParams callback) { // 检测空变量 if (string.IsNullOrEmpty(msgName)) { Debug.LogError("RegisterMsg:" + msgName + " is Null or Empty"); return; } // 检测空变量 if (null == callback) { Debug.LogError("RegisterMsg:" + msgName + " callback is Null"); return; } GoRegisterMsgByChannel(TMsgChannel.Global, msgName, callback); }
/// <summary> /// 注册消息, /// 注意第一个参数,使用了C# this的扩展, /// 所以只有实现IMsgReceiver的对象才能调用此方法 /// </summary> public static void RegisterLogicMsg(this IMsgReceiver self, string msgName, VoidDelegate.WithParams callback) { // 略过 if (string.IsNullOrEmpty(msgName)) { QPrint.FrameworkWarn("RegisterMsg:" + msgName + " is Null or Empty"); return; } // 略过 if (null == callback) { QPrint.FrameworkWarn("RegisterMsg:" + msgName + " callback is Null"); return; } // 略过 if (!mMsgHandlerDict.ContainsKey(msgName)) { mMsgHandlerDict [msgName] = new List <LogicMsgHandler> (); } // 看下这里 var handlers = mMsgHandlerDict [msgName]; // 略过 // 防止重复注册 foreach (var handler in handlers) { if (handler.receiver == self && handler.callback == callback) { QPrint.FrameworkWarn("RegisterMsg:" + msgName + " ayready Register"); return; } } // 再看下这里 handlers.Add(new LogicMsgHandler(self, callback)); }
//注册消息 /// <summary> /// 注册消息 -- 自定义类型模式 /// </summary> /// <param name="channel">消息类型</param> /// <param name="msgName">消息名称</param> /// <param name="callback">回调</param> protected void GoRegisterMsgByChannel(TMsgChannel channel, string msgName, VoidDelegate.WithParams callback) { // 检测空变量 if (string.IsNullOrEmpty(msgName)) { Debug.LogError("RegisterMsg:" + msgName + " is Null or Empty"); return; } // 检测空变量 if (null == callback) { Debug.LogError("RegisterMsg:" + msgName + " callback is Null"); return; } //如果词典为空 则新建 if (m_msgHandlerDict == null) { m_msgHandlerDict = new Dictionary <TMsgChannel, List <string> >(); } // 添加消息通道 if (!m_msgHandlerDict.ContainsKey(TMsgChannel.Global)) { m_msgHandlerDict[channel] = new List <string>(); } // 检测是否存在 不存在 则添加 if (!m_msgHandlerDict[channel].Contains(msgName)) { m_msgHandlerDict[channel].Add(msgName); } this.RegisterMsgByChannel(channel, msgName, callback); }
/// <summary> /// 注销逻辑消息 /// </summary> public static void UnRegisterLogicMsg(this IMsgReceiver self, string msgName, VoidDelegate.WithParams callback) { if (string.IsNullOrEmpty(msgName)) { QPrint.FrameworkError("UnRegisterMsg is Null or Empty"); return; } if (null == callback) { QPrint.FrameworkError("RegisterMsg callback is Null"); return; } var handlers = mMsgHandlerDict [msgName]; int handlerCount = handlers.Count; // 删除List需要从后向前遍历 for (int index = handlerCount - 1; index >= 0; index--) { var handler = handlers [index]; if (handler.receiver == self && handler.callback == callback) { handlers.Remove(handler); break; } } }
/* * VoidDelegate.WithParams 是一种委托 ,定义是这样的 * * public class VoidDelegate{ * public delegate void WithParams(params object[] paramList); * } */ public LogicMsgHandler(IMsgReceiver receiver, VoidDelegate.WithParams callback) { this.receiver = receiver; this.callback = callback; }
/* * VoidDelegate.WithParams 是一种委托 ,定义是这样的 * * public class VoidDelegate{ * public delegate void WithParams(params object[] paramList); * } */ public QMsgHandler(IMsgReceiver receiver,VoidDelegate.WithParams callback) { this.receiver = receiver; this.callback = callback; }