예제 #1
0
        /// <summary>
        /// 其实注销消息只需要Object和Go就足够了 不需要callback
        /// </summary>
        public static void UnRegisterMsgByChannel(this IMsgReceiver self, QMgrID channel, string msgName)
        {
            if (CheckStrNullOrEmpty(msgName))
            {
                return;
            }

            if (!mMsgHandlerDict.ContainsKey(channel))
            {
                Debug.LogError("Channel:" + channel.ToString() + " doesn't exist");
                return;
            }

            var handlers = mMsgHandlerDict[channel] [msgName];

            int handlerCount = handlers.Count;

            // 删除List需要从后向前遍历
            for (int index = handlerCount - 1; index >= 0; index--)
            {
                var handler = handlers [index];
                if (handler.receiver == self)
                {
                    handlers.Remove(handler);
                    break;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 注册消息,
        /// 注意第一个参数,使用了C# this的扩展,
        /// 所以只有实现IMsgReceiver的对象才能调用此方法
        /// </summary>
        public static void RegisterMsgByChannel(this IMsgReceiver self, QMgrID channel, string msgName, QVoidDelegate.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(channel))
            {
                mMsgHandlerDict [channel] = new Dictionary <string, List <QMsgHandler> > ();
            }

            // 略过
            if (!mMsgHandlerDict[channel].ContainsKey(msgName))
            {
                mMsgHandlerDict[channel] [msgName] = new List <QMsgHandler> ();
            }

            // 看下这里
            var handlers = mMsgHandlerDict [channel][msgName];

            // 略过
            // 防止重复注册
            foreach (var handler in handlers)
            {
                if (handler.receiver == self && handler.callback == callback)
                {
                    Debug.LogWarning("RegisterMsg:" + msgName + " ayready Register");
                    return;
                }
            }

            // 再看下这里
            handlers.Add(new QMsgHandler(self, callback));
        }
예제 #3
0
        public static void SendMsgByChannel(this IMsgSender sender, QMgrID channel, string msgName, params object[] paramList)
        {
            if (CheckStrNullOrEmpty(msgName))
            {
                return;
            }

            if (!mMsgHandlerDict.ContainsKey(channel))
            {
                Debug.LogError("Channel:" + channel.ToString() + " doesn't exist");
                return;
            }

            // 略过,不用看
            if (!mMsgHandlerDict[channel].ContainsKey(msgName))
            {
                Debug.LogWarning("SendMsg is UnRegister");
                return;
            }

            // 开始看!!!!
            var handlers = mMsgHandlerDict[channel][msgName];

            var handlerCount = handlers.Count;

            // 之所以是从后向前遍历,是因为  从前向后遍历删除后索引值会不断变化
            // 参考文章,http://www.2cto.com/kf/201312/266723.html
            for (int index = handlerCount - 1; index >= 0; index--)
            {
                var handler = handlers[index];

                if (handler.receiver != null)
                {
                    Debug.Log("SendLogicMsg:" + msgName + " Succeed");
                    handler.callback(paramList);
                }
                else
                {
                    handlers.Remove(handler);
                }
            }
        }
예제 #4
0
        public static void UnRegisterMsg(this IMsgReceiver self, string msgName, QVoidDelegate.WithParams callback, QMgrID channel = QMgrID.Global)
        {
            if (CheckStrNullOrEmpty(msgName) || CheckDelegateNull(callback))
            {
                return;
            }

            // 添加通道
            if (!mMsgHandlerDict.ContainsKey(channel))
            {
                Debug.LogError("Channel:" + channel.ToString() + " doesn't exist");
                return;
            }

            var handlers = mMsgHandlerDict [channel] [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;
                }
            }
        }
예제 #5
0
        public static void RegisterLogicMsg(this IMsgReceiver self, string msgName, QVoidDelegate.WithParams callback, QMgrID channel = QMgrID.Global)
        {
            if (CheckStrNullOrEmpty(msgName) || CheckDelegateNull(callback))
            {
                return;
            }

            // 添加消息通道
            if (!mMsgHandlerDict.ContainsKey(channel))
            {
                mMsgHandlerDict [channel] = new Dictionary <string, List <QMsgHandler> > ();
            }

            // 略过
            if (!mMsgHandlerDict[channel].ContainsKey(msgName))
            {
                mMsgHandlerDict[channel] [msgName] = new List <QMsgHandler> ();
            }

            // 看下这里
            var handlers = mMsgHandlerDict [channel][msgName];

            // 略过
            // 防止重复注册
            foreach (var handler in handlers)
            {
                if (handler.receiver == self && handler.callback == callback)
                {
                    Debug.LogWarning("RegisterMsg:" + msgName + " ayready Register");
                    return;
                }
            }

            // 再看下这里
            handlers.Add(new QMsgHandler(self, callback));
        }