diff options
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Tools/ScopedEvent.cs')
-rw-r--r-- | WorldlineKeepers/Assets/Scripts/Tools/ScopedEvent.cs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Tools/ScopedEvent.cs b/WorldlineKeepers/Assets/Scripts/Tools/ScopedEvent.cs new file mode 100644 index 0000000..23bfaa8 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/ScopedEvent.cs @@ -0,0 +1,54 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.AI; + +namespace WK +{ + + /// <summary> + /// 类似UnityEvent但是更轻量 + /// </summary> + public class ScopedEvent + { + public delegate void EventHandler(params object[] args); + + private List<EventHandler> m_Handlers = new List<EventHandler>(); + + public void AddListener(EventHandler handler) + { + if(handler == null) + { + return; + } + if (m_Handlers.Contains(handler)) + return; + m_Handlers.Add(handler); + } + + public void RemoveListener(EventHandler handler) + { + + if (handler == null) + { + return; + } + m_Handlers.Remove(handler); + } + + public bool HasHandler(EventHandler handler) + { + return m_Handlers.Contains(handler); + } + + public void Invoke(params object[] args) + { + for(int i = 0; i < m_Handlers.Count; ++i) + { + m_Handlers[i](args); + } + } + + } + +} |