summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs
blob: c9cde83cc8a93a24dd66bf1d662bbd557bdba3e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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);
                    }
                }
            }
        }

    }
}