您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
57 行
1.7 KiB
57 行
1.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace GameplayIngredients
|
|
{
|
|
public static class Messager
|
|
{
|
|
public delegate void Message();
|
|
|
|
private static Dictionary<string, Message> m_RegisteredMessages;
|
|
|
|
static Messager()
|
|
{
|
|
m_RegisteredMessages = new Dictionary<string, Message>();
|
|
}
|
|
|
|
public static void RegisterMessage(string eventName, Message message)
|
|
{
|
|
if (!m_RegisteredMessages.ContainsKey(eventName))
|
|
m_RegisteredMessages.Add(eventName, message);
|
|
else
|
|
m_RegisteredMessages[eventName] += message;
|
|
}
|
|
|
|
public static void RemoveMessage(string eventName, Message message)
|
|
{
|
|
var currentEvent = m_RegisteredMessages[eventName];
|
|
currentEvent -= message;
|
|
if (currentEvent == null || currentEvent.GetInvocationList().Length == 0)
|
|
m_RegisteredMessages.Remove(eventName);
|
|
}
|
|
|
|
public static void Send(string eventName)
|
|
{
|
|
Debug.Log(string.Format("[MessageManager] Broadcast: {0}", eventName));
|
|
|
|
if (m_RegisteredMessages.ContainsKey(eventName))
|
|
{
|
|
try
|
|
{
|
|
m_RegisteredMessages[eventName]();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError(string.Format("Messager : Caught {0} while sending Message {1}", e.GetType().Name, eventName));
|
|
Debug.LogException(e);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("[MessageManager] could not find any listeners for event : " + eventName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|