using System; using System.Collections.Generic; using UnityEngine; public static class EventCenter { // 使用 enum 作为事件名的 key private static Dictionary> eventDict = new Dictionary>(); /// 添加监听器 public static void AddListener(EventType eventType, Action callback) { if (eventDict.ContainsKey(eventType)) eventDict[eventType] += callback; else eventDict[eventType] = callback; } /// 移除监听器 public static void RemoveListener(EventType eventType, Action callback) { if (eventDict.ContainsKey(eventType)) { eventDict[eventType] -= callback; if (eventDict[eventType] == null) eventDict.Remove(eventType); } } /// 触发事件 public static void Trigger(EventType eventType, object param = null) { if (eventDict.TryGetValue(eventType, out var callback)) { callback?.Invoke(param); } } /// 清空所有监听 public static void Clear() { eventDict.Clear(); } }