/// <summary> /// 添加消息处理函数 /// </summary> /// <param name="msgID">消息ID</param> /// <param name="msgProc">消息处理函数</param> public void AddMsgHandler(int msgID, MsgHandler msgProc) { // Debug.Log("添加消息ID: " + msgID); if (msgProc == null) { return; } /*foreach(KeyValuePair<int, MsgHandler> kvp in m_msgHandlerMap) * { * int key = kvp.Key; * Debug.Log("消息:" + key); * }*/ if (m_msgHandlerMap.ContainsKey(msgID) == false) { // 注册消息处理函数 m_msgHandlerMap.Add(msgID, msgProc); // 注入消息路由表 MsgHandlerMap.Add(msgID, ID); } else { } }
/// <summary> /// 消息处理函数,转发消息 /// </summary> /// <param name="msg">消息</param> public override void MsgProc(Msg msg) { // 定向发送,只让某个引擎处理该消息 if (msg.ReceiverEngineID != EngineID.Unknow) { EngineManager.SendMsg(msg, msg.ReceiverEngineID); } else // 广播 { List <EngineID> engineIDs = MsgHandlerMap.GetEngineIDList(msg.ID); if (engineIDs != null) { if (engineIDs.Count == 1) { EngineManager.SendMsg(msg, engineIDs[0]); } else // 广播对象大于1个的时候,消息必须采用复制形式发送,这样在各个处理引擎处理 // 完释放后,才能正确的释放回对象池 { for (int i = 0; i < engineIDs.Count; ++i) { if (i == engineIDs.Count - 1) // 最后一个消息,使用本体发过去 { EngineManager.SendMsg(msg, engineIDs[i]); } else { EngineManager.SendMsg(Msg.Create(msg), engineIDs[i]); } } } } else { } } }
/// <summary> /// 删除消息处理函数 /// </summary> /// <param name="msgID">消息ID</param> public void RemoveMsgHandler(int msgID) { m_msgHandlerMap.Remove(msgID); MsgHandlerMap.Remove(msgID, ID); }