diff options
author | chai <215380520@qq.com> | 2023-05-15 09:28:11 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-05-15 09:28:11 +0800 |
commit | 3b036c6de871aa519a1f7fbfb52e09618945041f (patch) | |
tree | d5dc6d4f1d501e4ce3c6d69ca7a698a03634490c /WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs | |
parent | 6fb204d494b897907d655b5752196983a82ceba2 (diff) |
*misc
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs')
-rw-r--r-- | WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs b/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs new file mode 100644 index 0000000..0002f14 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs @@ -0,0 +1,89 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace WK +{ + + public class ScopedNotification + { + public delegate void NotificatonHandler(params object[] args); + + private Dictionary<string, List<NotificatonHandler>> m_EventListeners = new Dictionary<string, List<NotificatonHandler>>(); + + public void AddObserver(string eventName, NotificatonHandler handler) + { + if (handler == null) + { + return; + } + + if (string.IsNullOrEmpty(eventName)) + { + return; + } + + List<NotificatonHandler> handlers; + if (!m_EventListeners.ContainsKey(eventName)) + { + m_EventListeners.Add(eventName, new List<NotificatonHandler>()); + } + + handlers = m_EventListeners[eventName]; + handlers.Add(handler); + } + + public void RemoveObserver(string eventName, NotificatonHandler handler) + { + if(handler == null) { return; } + + if(string.IsNullOrEmpty(eventName)) { return; } + + if (!m_EventListeners.ContainsKey(eventName)) + return; + + List<NotificatonHandler> handlers = m_EventListeners[eventName]; + if(handlers.Contains(handler)) + handlers.Remove(handler); + } + + public void RemoveEvent(string eventName) + { + if (string.IsNullOrEmpty(eventName)) { return; } + if(m_EventListeners.ContainsKey(eventName)) + { + m_EventListeners.Remove(eventName); + } + } + + public void Clean() + { + m_EventListeners.Clear(); + } + + public void PostNotification(string eventName, params object[] args) + { + if (string.IsNullOrEmpty(eventName)) { return; } + + if (!m_EventListeners.ContainsKey(eventName)) + return; + + List<NotificatonHandler> handlers = m_EventListeners[eventName]; + for(int i = 0; i < handlers.Count; i++) + { + var handler = handlers[i]; + if(handler != null) + { + handler(args); + } + } + } + + public void PostNotification(string eventName) + { + PostNotification(eventName, null); + } + + } + +}
\ No newline at end of file |