From 6fb204d494b897907d655b5752196983a82ceba2 Mon Sep 17 00:00:00 2001
From: chai <215380520@qq.com>
Date: Sat, 13 May 2023 15:20:20 +0800
Subject: *misc
---
.../Assets/Scripts/Tools/GlobalEventManager.cs | 97 ++++++++++++
.../Scripts/Tools/GlobalEventManager.cs.meta | 11 ++
.../Assets/Scripts/Tools/NotificationCenter.cs | 167 +++++++++++++++++++++
.../Scripts/Tools/NotificationCenter.cs.meta | 11 ++
4 files changed, 286 insertions(+)
create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs
create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs.meta
create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs
create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs.meta
(limited to 'WorldlineKeepers/Assets/Scripts/Tools')
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
+{
+ ///
+ /// 全局事件
+ ///
+ public class GlobalEventManager : Singleton
+ {
+
+ // callback
+ public delegate void EventCallback(params object[] objs);
+ public Dictionary> AllEvents = new Dictionary>();
+
+ public void Register(string eventName, EventCallback callback, bool addFirst = false)
+ {
+ if (callback == null)
+ {
+ Debug.LogError("监听函数不能为空");
+ return;
+ }
+ LinkedList list;
+ if (!AllEvents.TryGetValue(eventName, out list))
+ {
+ list = new LinkedList(); // 这里最好从池子里拿
+ 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 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 list;
+ if (AllEvents.TryGetValue(eventName, out list) && list != null && list.Count > 0)
+ {
+ foreach (EventCallback callback in list)
+ {
+ if (callback != null)
+ {
+ callback.Invoke(objs);
+ }
+ }
+ }
+ }
+
+ }
+}
diff --git a/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs.meta
new file mode 100644
index 0000000..0c2c8ec
--- /dev/null
+++ b/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 7a9f38c8080be5e41ad5220346ba8c7f
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs b/WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs
new file mode 100644
index 0000000..b3f299a
--- /dev/null
+++ b/WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs
@@ -0,0 +1,167 @@
+using System;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace WK
+{
+
+ public class NotificationCenter
+ {
+ private Dictionary>>> _table = new Dictionary>>>();
+
+ private HashSet>> _invoking = new HashSet>>();
+
+ public static readonly NotificationCenter instance = new NotificationCenter();
+
+ private NotificationCenter()
+ {
+ }
+
+ public void AddObserver(Action