using System;
|
using System.Collections.Generic;
|
using UnityEngine;
|
|
/// <summary>
|
/// 事件广播系统
|
/// </summary>
|
public class EventBroadcast
|
{
|
private static EventBroadcast _instance;
|
public static EventBroadcast Instance
|
{
|
get
|
{
|
if (_instance == null)
|
{
|
_instance = new EventBroadcast();
|
}
|
return _instance;
|
}
|
}
|
|
// 事件字典,存储事件ID和对应的委托列表
|
private Dictionary<string, Delegate> eventDict = new Dictionary<string, Delegate>();
|
|
// 防止外部实例化
|
private EventBroadcast() { }
|
|
#region 添加监听
|
|
/// <summary>
|
/// 添加无参事件监听
|
/// </summary>
|
/// <param name="eventId">事件ID</param>
|
/// <param name="callback">回调函数</param>
|
public void AddListener(string eventId, Action callback)
|
{
|
OnListenerAdding(eventId, callback);
|
eventDict[eventId] = (Action)eventDict[eventId] + callback;
|
}
|
|
/// <summary>
|
/// 添加单参数事件监听
|
/// </summary>
|
public void AddListener<T>(string eventId, Action<T> callback)
|
{
|
OnListenerAdding(eventId, callback);
|
eventDict[eventId] = (Action<T>)eventDict[eventId] + callback;
|
}
|
|
/// <summary>
|
/// 添加双参数事件监听
|
/// </summary>
|
public void AddListener<T, U>(string eventId, Action<T, U> callback)
|
{
|
OnListenerAdding(eventId, callback);
|
eventDict[eventId] = (Action<T, U>)eventDict[eventId] + callback;
|
}
|
|
/// <summary>
|
/// 添加三参数事件监听
|
/// </summary>
|
public void AddListener<T, U, V>(string eventId, Action<T, U, V> callback)
|
{
|
OnListenerAdding(eventId, callback);
|
eventDict[eventId] = (Action<T, U, V>)eventDict[eventId] + callback;
|
}
|
|
#endregion
|
|
#region 移除监听
|
|
/// <summary>
|
/// 移除无参事件监听
|
/// </summary>
|
/// <param name="eventId">事件ID</param>
|
/// <param name="callback">回调函数</param>
|
public void RemoveListener(string eventId, Action callback)
|
{
|
if (OnListenerRemoving(eventId, callback))
|
{
|
eventDict[eventId] = (Action)eventDict[eventId] - callback;
|
OnListenerRemoved(eventId);
|
}
|
}
|
|
/// <summary>
|
/// 移除单参数事件监听
|
/// </summary>
|
public void RemoveListener<T>(string eventId, Action<T> callback)
|
{
|
if (OnListenerRemoving(eventId, callback))
|
{
|
eventDict[eventId] = (Action<T>)eventDict[eventId] - callback;
|
OnListenerRemoved(eventId);
|
}
|
}
|
|
/// <summary>
|
/// 移除双参数事件监听
|
/// </summary>
|
public void RemoveListener<T, U>(string eventId, Action<T, U> callback)
|
{
|
if (OnListenerRemoving(eventId, callback))
|
{
|
eventDict[eventId] = (Action<T, U>)eventDict[eventId] - callback;
|
OnListenerRemoved(eventId);
|
}
|
}
|
|
/// <summary>
|
/// 移除三参数事件监听
|
/// </summary>
|
public void RemoveListener<T, U, V>(string eventId, Action<T, U, V> callback)
|
{
|
if (OnListenerRemoving(eventId, callback))
|
{
|
eventDict[eventId] = (Action<T, U, V>)eventDict[eventId] - callback;
|
OnListenerRemoved(eventId);
|
}
|
}
|
|
#endregion
|
|
#region 触发事件
|
|
/// <summary>
|
/// 触发无参事件
|
/// </summary>
|
/// <param name="eventId">事件ID</param>
|
public void Broadcast(string eventId)
|
{
|
if (eventDict.TryGetValue(eventId, out Delegate d))
|
{
|
Action callback = d as Action;
|
if (callback != null)
|
{
|
callback();
|
}
|
else
|
{
|
Debug.LogError($"事件 {eventId} 的委托类型不匹配");
|
}
|
}
|
}
|
|
/// <summary>
|
/// 触发单参数事件
|
/// </summary>
|
public void Broadcast<T>(string eventId, T arg)
|
{
|
if (eventDict.TryGetValue(eventId, out Delegate d))
|
{
|
Action<T> callback = d as Action<T>;
|
if (callback != null)
|
{
|
callback(arg);
|
}
|
else
|
{
|
Debug.LogError($"事件 {eventId} 的委托类型不匹配");
|
}
|
}
|
}
|
|
/// <summary>
|
/// 触发双参数事件
|
/// </summary>
|
public void Broadcast<T, U>(string eventId, T arg1, U arg2)
|
{
|
if (eventDict.TryGetValue(eventId, out Delegate d))
|
{
|
Action<T, U> callback = d as Action<T, U>;
|
if (callback != null)
|
{
|
callback(arg1, arg2);
|
}
|
else
|
{
|
Debug.LogError($"事件 {eventId} 的委托类型不匹配");
|
}
|
}
|
}
|
|
/// <summary>
|
/// 触发三参数事件
|
/// </summary>
|
public void Broadcast<T, U, V>(string eventId, T arg1, U arg2, V arg3)
|
{
|
if (eventDict.TryGetValue(eventId, out Delegate d))
|
{
|
Action<T, U, V> callback = d as Action<T, U, V>;
|
if (callback != null)
|
{
|
callback(arg1, arg2, arg3);
|
}
|
else
|
{
|
Debug.LogError($"事件 {eventId} 的委托类型不匹配");
|
}
|
}
|
}
|
|
#endregion
|
|
#region 辅助方法
|
|
/// <summary>
|
/// 添加监听前的处理
|
/// </summary>
|
private void OnListenerAdding(string eventId, Delegate callback)
|
{
|
if (!eventDict.ContainsKey(eventId))
|
{
|
eventDict.Add(eventId, null);
|
}
|
|
Delegate d = eventDict[eventId];
|
if (d != null && d.GetType() != callback.GetType())
|
{
|
Debug.LogError($"尝试为事件 {eventId} 添加不同类型的委托,当前类型:{d.GetType()},尝试添加类型:{callback.GetType()}");
|
}
|
}
|
|
/// <summary>
|
/// 移除监听前的处理
|
/// </summary>
|
private bool OnListenerRemoving(string eventId, Delegate callback)
|
{
|
if (!eventDict.ContainsKey(eventId))
|
{
|
Debug.LogWarning($"尝试移除不存在的事件 {eventId}");
|
return false;
|
}
|
|
Delegate d = eventDict[eventId];
|
if (d == null)
|
{
|
Debug.LogWarning($"尝试移除事件 {eventId} 的空委托");
|
return false;
|
}
|
else if (d.GetType() != callback.GetType())
|
{
|
Debug.LogError($"尝试为事件 {eventId} 移除不同类型的委托,当前类型:{d.GetType()},尝试移除类型:{callback.GetType()}");
|
return false;
|
}
|
|
return true;
|
}
|
|
/// <summary>
|
/// 移除监听后的处理
|
/// </summary>
|
private void OnListenerRemoved(string eventId)
|
{
|
if (eventDict[eventId] == null)
|
{
|
eventDict.Remove(eventId);
|
}
|
}
|
|
/// <summary>
|
/// 清空所有事件
|
/// </summary>
|
public void ClearAllEvents()
|
{
|
eventDict.Clear();
|
}
|
|
#endregion
|
}
|