WXMC/proj/unity/Assets/Scripts/Message/MessageDispatcher.cs
2024-12-04 16:18:46 +08:00

72 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**************************************************************************************************
Copyright (C) 2021 - All Rights Reserved.
--------------------------------------------------------------------------------------------------------
当前版本1.0;
文 件 MessageDispatcher.cs;
作 者 HuBingJie;
时 间 2021 - 06 - 22;
注 释 消息中心,方便各系统之间通信;
**************************************************************************************************/
using System.Collections.Generic;
public delegate void MessageHandler(uint iMessageType, object arg);
public class MessageDispatcher : DataSingleton<MessageDispatcher>
{
Dictionary<uint, MessageHandler> m_HandlerMap;
public MessageDispatcher()
{
m_HandlerMap = new Dictionary<uint, MessageHandler>();
}
public MessageHandler RegisterMessageHandler(uint iMessageType, MessageHandler handler)
{
if (handler == null)
return null;
if (!m_HandlerMap.ContainsKey(iMessageType))
{
MessageHandler Handlers = handler;
m_HandlerMap.Add(iMessageType, Handlers);
}
else
{
m_HandlerMap[iMessageType] += handler;
}
return handler;
}
public void UnRegisterMessageHandler(uint iMessageType, MessageHandler handler)
{
if (handler == null)
return;
if (m_HandlerMap.ContainsKey(iMessageType))
{
m_HandlerMap[iMessageType] -= handler;
}
}
public void SendMessage(uint iMessageType, object arg)
{
if (m_HandlerMap.ContainsKey(iMessageType))
{
if (m_HandlerMap[iMessageType] != null)
{
m_HandlerMap[iMessageType].Invoke(iMessageType, arg);
}
}
}
public override void Init()
{
}
public override void OnDispose()
{
throw new System.NotImplementedException();
}
}