using System; using System.Collections; using System.Collections.Generic; public class EventGroup { private readonly Dictionary>> _cachedListener = new Dictionary>>(); /// /// 添加一个监听 /// public void AddListener(System.Action listener) where TEvent : IEventMessage { System.Type eventType = typeof(TEvent); if (_cachedListener.ContainsKey(eventType) == false) _cachedListener.Add(eventType, new List>()); if (_cachedListener[eventType].Contains(listener) == false) { _cachedListener[eventType].Add(listener); EventManager.AddListener(eventType, listener); } else { UnityEngine.Debug.LogWarning($"Event listener is exist : {eventType}"); } } /// /// 移除所有缓存的监听 /// public void RemoveAllListener() { foreach (var pair in _cachedListener) { System.Type eventType = pair.Key; for (int i = 0; i < pair.Value.Count; i++) { EventManager.RemoveListener(eventType, pair.Value[i]); } pair.Value.Clear(); } _cachedListener.Clear(); } }