Example #1
0
 void Update()
 {
     if (idleMsgHandler.Count > 0)
     {
         IdleMsg iMsg = idleMsgHandler.Dequeue();
         iMsg.Callback(iMsg.Param);
     }
 }
Example #2
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msgName"></param>
        /// <param name="paramList"></param>
        public void SendMsg(string msgName, object param)
        {
            if (!msgHandlerDict.ContainsKey(msgName))
            {
                Debug.LogError("该消息名没有被注册:" + msgName);
                return;
            }
            var handlers = msgHandlerDict[msgName];

            //从后向前遍历,删除item后前面item的索引不会变化
            for (int i = handlers.Count - 1; i >= 0; i--)
            {
                MsgHandler handler = handlers[i];
                if (handler.Receiver != null)
                {
                    IdleMsg iMsg = new IdleMsg(param, handler.Callback);
                    idleMsgHandler.Enqueue(iMsg);
                }
                else
                {
                    handlers.Remove(handler);
                }
            }
        }