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