WXMC/proj/unity/Assets/Scripts/Message/MessageDispatcher.cs

72 lines
2.0 KiB
C#
Raw Normal View History

2024-12-04 16:18:46 +08:00
/**************************************************************************************************
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();
}
}