diff options
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Tools')
4 files changed, 286 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); + } + } + } + } + + } +} 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<string, Dictionary<object, List<Action<object, object>>>> _table = new Dictionary<string, Dictionary<object, List<Action<object, object>>>>(); + + private HashSet<List<Action<object, object>>> _invoking = new HashSet<List<Action<object, object>>>(); + + public static readonly NotificationCenter instance = new NotificationCenter(); + + private NotificationCenter() + { + } + + public void AddObserver(Action<object, object> handler, string notificationName) + { + AddObserver(handler, notificationName, null); + } + + public void AddObserver(Action<object, object> handler, string notificationName, object sender) + { + if (handler == null) + { + Debug.LogError("Can't add a null event handler for notification, " + notificationName); + return; + } + if (string.IsNullOrEmpty(notificationName)) + { + Debug.LogError("Can't observe an unnamed notification"); + return; + } + if (!_table.ContainsKey(notificationName)) + { + _table.Add(notificationName, new Dictionary<object, List<Action<object, object>>>()); + } + Dictionary<object, List<Action<object, object>>> dictionary = _table[notificationName]; + object key = ((sender != null) ? sender : this); + if (!dictionary.ContainsKey(key)) + { + dictionary.Add(key, new List<Action<object, object>>()); + } + List<Action<object, object>> list = dictionary[key]; + if (_invoking.Contains(list)) + { + list = (dictionary[key] = new List<Action<object, object>>(list)); + } + list.Add(handler); + } + + public void RemoveObserver(Action<object, object> handler, string notificationName) + { + RemoveObserver(handler, notificationName, null); + } + + public void RemoveObserver(Action<object, object> handler, string notificationName, object sender) + { + if (handler == null) + { + Debug.LogError("Can't remove a null event handler for notification, " + notificationName); + } + else if (string.IsNullOrEmpty(notificationName)) + { + Debug.LogError("A notification name is required to stop observation"); + } + else + { + if (!_table.ContainsKey(notificationName)) + { + return; + } + Dictionary<object, List<Action<object, object>>> dictionary = _table[notificationName]; + object key = ((sender != null) ? sender : this); + if (!dictionary.ContainsKey(key)) + { + return; + } + List<Action<object, object>> list = dictionary[key]; + int num = list.IndexOf(handler); + if (num != -1) + { + if (_invoking.Contains(list)) + { + list = (dictionary[key] = new List<Action<object, object>>(list)); + } + list.RemoveAt(num); + } + } + } + + public void Clean() + { + string[] array = new string[_table.Keys.Count]; + _table.Keys.CopyTo(array, 0); + for (int num = array.Length - 1; num >= 0; num--) + { + string key = array[num]; + Dictionary<object, List<Action<object, object>>> dictionary = _table[key]; + object[] array2 = new object[dictionary.Keys.Count]; + dictionary.Keys.CopyTo(array2, 0); + for (int num2 = array2.Length - 1; num2 >= 0; num2--) + { + object key2 = array2[num2]; + if (dictionary[key2].Count == 0) + { + dictionary.Remove(key2); + } + } + if (dictionary.Count == 0) + { + _table.Remove(key); + } + } + } + + public void PostNotification(string notificationName) + { + PostNotification(notificationName, null); + } + + public void PostNotification(string notificationName, object sender) + { + PostNotification(notificationName, sender, null); + } + + public void PostNotification(string notificationName, object sender, object e) + { + if (string.IsNullOrEmpty(notificationName)) + { + Debug.LogError("A notification name is required"); + } + else + { + if (!_table.ContainsKey(notificationName)) + { + return; + } + Dictionary<object, List<Action<object, object>>> dictionary = _table[notificationName]; + if (sender != null && dictionary.ContainsKey(sender)) + { + List<Action<object, object>> list = dictionary[sender]; + _invoking.Add(list); + for (int i = 0; i < list.Count; i++) + { + list[i](sender, e); + } + _invoking.Remove(list); + } + if (dictionary.ContainsKey(this)) + { + List<Action<object, object>> list2 = dictionary[this]; + _invoking.Add(list2); + for (int j = 0; j < list2.Count; j++) + { + list2[j](sender, e); + } + _invoking.Remove(list2); + } + } + } + } + +} diff --git a/WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs.meta new file mode 100644 index 0000000..13d54ee --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f222398a57afbf446b471ccf17bb9575 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |