Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

72 行
2.6 KiB

using System;
using System.Collections.Generic;
using UnityEngine;
namespace GameplayIngredients
{
public static class Messager
{
public delegate void Message(GameObject instigator = null);
private static Dictionary<string, List<Message>> m_RegisteredMessages;
static Messager()
{
m_RegisteredMessages = new Dictionary<string, List<Message>>();
}
public static void RegisterMessage(string messageName, Message message)
{
if (!m_RegisteredMessages.ContainsKey(messageName))
m_RegisteredMessages.Add(messageName, new List<Message>());
if (!m_RegisteredMessages[messageName].Contains(message))
m_RegisteredMessages[messageName].Add(message);
else
{
Debug.LogWarning(string.Format("Messager : {0} entry already contains reference to message.", messageName));
}
}
public static void RemoveMessage(string messageName, Message message)
{
var currentEvent = m_RegisteredMessages[messageName];
if(currentEvent.Contains(message))
currentEvent.Remove(message);
if (currentEvent == null || currentEvent.Count == 0)
m_RegisteredMessages.Remove(messageName);
}
public static void Send(string eventName, GameObject instigator = null)
{
if(GameplayIngredientsSettings.currentSettings.verboseCalls)
Debug.Log(string.Format("[MessageManager] Broadcast: {0}", eventName));
if (m_RegisteredMessages.ContainsKey(eventName))
{
try
{
// Get a copy of registered messages to iterate on. This prevents issues while deregistering message recievers while iterating.
var messages = m_RegisteredMessages[eventName].ToArray();
foreach (var message in messages)
{
if(message != null)
message.Invoke(instigator);
}
}
catch (Exception e)
{
Debug.LogError(string.Format("Messager : Caught {0} while sending Message {1}", e.GetType().Name, eventName));
Debug.LogException(e);
}
}
else
{
if(GameplayIngredientsSettings.currentSettings.verboseCalls)
Debug.Log("[MessageManager] could not find any listeners for event : " + eventName);
}
}
}
}