using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace BigSpace.XRCore.Event { public class GlobalEventMgr { private static Dictionary> events = new Dictionary>(); /// /// 添加监听事件 /// /// 监听事件 /// 监听回调 public static void Listen(System.Enum type, Action action) { List listeners; if (!events.TryGetValue(type, out listeners)) { listeners = new List(); events.Add(type, listeners); } listeners.Add(action); } /// /// 带一个参数的监听 /// 用泛型避免使用 params object[] args 带来装箱和拆箱的性能损耗 /// /// /// /// public static void Listen(System.Enum type, Action action) { List listeners; if (!events.TryGetValue(type, out listeners)) { listeners = new List(); events.Add(type, listeners); } listeners.Add(action); } //两个参数监听回调 public static void Listen(System.Enum type, Action action) { List listeners; if (!events.TryGetValue(type, out listeners)) { listeners = new List(); events.Add(type, listeners); } listeners.Add(action); } //三个参数监听回调 public static void Listen(System.Enum type, Action action) { List listeners; if (!events.TryGetValue(type, out listeners)) { listeners = new List(); events.Add(type, listeners); } listeners.Add(action); } //四个参数监听回调 public static void Listen(System.Enum type, Action action) { List listeners; if (!events.TryGetValue(type, out listeners)) { listeners = new List(); events.Add(type, listeners); } listeners.Add(action); } /// /// 移除所有监听 /// /// public static void Remove(System.Enum type) { List listeners; if (events.TryGetValue(type, out listeners)) { events.Remove(type); } } /// /// 移除指定的监听 /// public static void Remove(System.Enum type, Action action) { //移除指定的监听 List listeners; if (events.TryGetValue(type, out listeners)) { if (listeners.Contains(action)) { listeners.Remove(action); } if (listeners.Count <= 0) { Remove(type); } } } public static void Remove(System.Enum type, Action action) { //移除指定的监听 List listeners; if (events.TryGetValue(type, out listeners)) { if (listeners.Contains(action)) { listeners.Remove(action); } if (listeners.Count <= 0) { Remove(type); } } } public static void Remove(System.Enum type, Action action) { //移除指定的监听 List listeners; if (events.TryGetValue(type, out listeners)) { if (listeners.Contains(action)) { listeners.Remove(action); } if (listeners.Count <= 0) { Remove(type); } } } public static void Remove(System.Enum type, Action action) { //移除指定的监听 List listeners; if (events.TryGetValue(type, out listeners)) { if (listeners.Contains(action)) { listeners.Remove(action); } if (listeners.Count <= 0) { Remove(type); } } } public static void Remove(System.Enum type, Action action) { //移除指定的监听 List listeners; if (events.TryGetValue(type, out listeners)) { if (listeners.Contains(action)) { listeners.Remove(action); } if (listeners.Count <= 0) { Remove(type); } } } /// /// 调用监听事件 /// public static void Dispatch(System.Enum type) { //Debug.Log($"globalevent0 type: {type}"); List listeners; if (events.TryGetValue(type, out listeners)) { for (var i = listeners.Count - 1; i >= 0; i--) { Action action = listeners[i] as Action; action?.Invoke(); } } } public static void Dispatch(System.Enum type, T arg0) { //Debug.Log($"globalevent1 type: {type}"); List listeners; if (events.TryGetValue(type, out listeners)) { for (var i = listeners.Count - 1; i >= 0; i--) { Action action = listeners[i] as Action; action?.Invoke(arg0); } } } public static void Dispatch(System.Enum type, T arg0, U arg1) { //Debug.Log($"globalevent2 type: {type}"); List listeners; if (events.TryGetValue(type, out listeners)) { for (var i = listeners.Count - 1; i >= 0; i--) { Action action = listeners[i] as Action; action?.Invoke(arg0, arg1); } } } public static void Dispatch(System.Enum type, T arg0, U arg1, V arg2) { List listeners; if (events.TryGetValue(type, out listeners)) { for (var i = listeners.Count - 1; i >= 0; i--) { Action action = listeners[i] as Action; action?.Invoke(arg0, arg1, arg2); } } } public static void Dispatch(System.Enum type, T arg0, U arg1, V arg2, Z arg3) { List listeners; if (events.TryGetValue(type, out listeners)) { for (var i = listeners.Count - 1; i >= 0; i--) { Action action = listeners[i] as Action; action?.Invoke(arg0, arg1, arg2, arg3); } } } } }