summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2023-05-13 15:20:20 +0800
committerchai <215380520@qq.com>2023-05-13 15:20:20 +0800
commit6fb204d494b897907d655b5752196983a82ceba2 (patch)
tree99b874414e9578cb68be5390b2cf08d2069ca77e /WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs
parent6c91f1ed6810a57da08a24dd1359f288c443dd75 (diff)
*misc
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs')
-rw-r--r--WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs97
1 files changed, 97 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs b/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs
new file mode 100644
index 0000000..47f3990
--- /dev/null
+++ b/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs
@@ -0,0 +1,97 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UIElements;
+
+namespace WK
+{
+ /// <summary>
+ /// 全局事件
+ /// </summary>
+ public class GlobalEventManager : Singleton<GlobalEventManager>
+ {
+
+ // callback
+ public delegate void EventCallback(params object[] objs);
+ public Dictionary<string, LinkedList<EventCallback>> AllEvents = new Dictionary<string, LinkedList<EventCallback>>();
+
+ public void Register(string eventName, EventCallback callback, bool addFirst = false)
+ {
+ if (callback == null)
+ {
+ Debug.LogError("监听函数不能为空");
+ return;
+ }
+ LinkedList<EventCallback> list;
+ if (!AllEvents.TryGetValue(eventName, out list))
+ {
+ list = new LinkedList<EventCallback>(); // 这里最好从池子里拿
+ AllEvents.Add(eventName, list);
+ }
+ if (!list.Contains(callback))
+ {
+ if (addFirst && list.Count > 0)
+ {
+ list.AddFirst(callback);
+ }
+ else
+ {
+ list.AddLast(callback);
+ }
+ }
+ else
+ {
+ Debug.LogError("重复添加监听, eventName=" + eventName);
+ }
+ }
+
+ public void UnRegister(string eventName, EventCallback callback)
+ {
+ if (callback == null)
+ {
+ Debug.LogError("监听函数不能为空");
+ return;
+ }
+ LinkedList<EventCallback> list;
+ if (!AllEvents.TryGetValue(eventName, out list))
+ {
+ return;
+ }
+ list.Remove(callback);
+ if (list.Count == 0)
+ {
+ AllEvents.Remove(eventName);
+ // 如果这里list是从池子里拿的,回收它
+ }
+ }
+
+ public void UnRegisterEvent(string eventName)
+ {
+ if (AllEvents.ContainsKey(eventName))
+ {
+ AllEvents.Remove(eventName);
+ }
+ }
+
+ public void UnRegisterAll(string eventName)
+ {
+ AllEvents.Remove(eventName);
+ }
+
+ public void Notify(string eventName, params object[] objs)
+ {
+ LinkedList<EventCallback> list;
+ if (AllEvents.TryGetValue(eventName, out list) && list != null && list.Count > 0)
+ {
+ foreach (EventCallback callback in list)
+ {
+ if (callback != null)
+ {
+ callback.Invoke(objs);
+ }
+ }
+ }
+ }
+
+ }
+}