yyl
2025-05-15 d341a47ed9bbb740f2ccb9d6d42fd74d5a1c6b5d
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections.Generic;
using UnityEngine;
 
/// <summary>
/// 事件广播系统
/// </summary>
public class EventBroadcast
{
    private static EventBroadcast _instance;
    public static EventBroadcast Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new EventBroadcast();
            }
            return _instance;
        }
    }
 
    // 事件字典,存储事件ID和对应的委托列表
    private Dictionary<string, Delegate> eventDict = new Dictionary<string, Delegate>();
 
    // 防止外部实例化
    private EventBroadcast() { }
 
    #region 添加监听
 
    /// <summary>
    /// 添加无参事件监听
    /// </summary>
    /// <param name="eventId">事件ID</param>
    /// <param name="callback">回调函数</param>
    public void AddListener(string eventId, Action callback)
    {
        OnListenerAdding(eventId, callback);
        eventDict[eventId] = (Action)eventDict[eventId] + callback;
    }
 
    /// <summary>
    /// 添加单参数事件监听
    /// </summary>
    public void AddListener<T>(string eventId, Action<T> callback)
    {
        OnListenerAdding(eventId, callback);
        eventDict[eventId] = (Action<T>)eventDict[eventId] + callback;
    }
 
    /// <summary>
    /// 添加双参数事件监听
    /// </summary>
    public void AddListener<T, U>(string eventId, Action<T, U> callback)
    {
        OnListenerAdding(eventId, callback);
        eventDict[eventId] = (Action<T, U>)eventDict[eventId] + callback;
    }
 
    /// <summary>
    /// 添加三参数事件监听
    /// </summary>
    public void AddListener<T, U, V>(string eventId, Action<T, U, V> callback)
    {
        OnListenerAdding(eventId, callback);
        eventDict[eventId] = (Action<T, U, V>)eventDict[eventId] + callback;
    }
 
    #endregion
 
    #region 移除监听
 
    /// <summary>
    /// 移除无参事件监听
    /// </summary>
    /// <param name="eventId">事件ID</param>
    /// <param name="callback">回调函数</param>
    public void RemoveListener(string eventId, Action callback)
    {
        if (OnListenerRemoving(eventId, callback))
        {
            eventDict[eventId] = (Action)eventDict[eventId] - callback;
            OnListenerRemoved(eventId);
        }
    }
 
    /// <summary>
    /// 移除单参数事件监听
    /// </summary>
    public void RemoveListener<T>(string eventId, Action<T> callback)
    {
        if (OnListenerRemoving(eventId, callback))
        {
            eventDict[eventId] = (Action<T>)eventDict[eventId] - callback;
            OnListenerRemoved(eventId);
        }
    }
 
    /// <summary>
    /// 移除双参数事件监听
    /// </summary>
    public void RemoveListener<T, U>(string eventId, Action<T, U> callback)
    {
        if (OnListenerRemoving(eventId, callback))
        {
            eventDict[eventId] = (Action<T, U>)eventDict[eventId] - callback;
            OnListenerRemoved(eventId);
        }
    }
 
    /// <summary>
    /// 移除三参数事件监听
    /// </summary>
    public void RemoveListener<T, U, V>(string eventId, Action<T, U, V> callback)
    {
        if (OnListenerRemoving(eventId, callback))
        {
            eventDict[eventId] = (Action<T, U, V>)eventDict[eventId] - callback;
            OnListenerRemoved(eventId);
        }
    }
 
    #endregion
 
    #region 触发事件
 
    /// <summary>
    /// 触发无参事件
    /// </summary>
    /// <param name="eventId">事件ID</param>
    public void Broadcast(string eventId)
    {
        if (eventDict.TryGetValue(eventId, out Delegate d))
        {
            Action callback = d as Action;
            if (callback != null)
            {
                callback();
            }
            else
            {
                Debug.LogError($"事件 {eventId} 的委托类型不匹配");
            }
        }
    }
 
    /// <summary>
    /// 触发单参数事件
    /// </summary>
    public void Broadcast<T>(string eventId, T arg)
    {
        if (eventDict.TryGetValue(eventId, out Delegate d))
        {
            Action<T> callback = d as Action<T>;
            if (callback != null)
            {
                callback(arg);
            }
            else
            {
                Debug.LogError($"事件 {eventId} 的委托类型不匹配");
            }
        }
    }
 
    /// <summary>
    /// 触发双参数事件
    /// </summary>
    public void Broadcast<T, U>(string eventId, T arg1, U arg2)
    {
        if (eventDict.TryGetValue(eventId, out Delegate d))
        {
            Action<T, U> callback = d as Action<T, U>;
            if (callback != null)
            {
                callback(arg1, arg2);
            }
            else
            {
                Debug.LogError($"事件 {eventId} 的委托类型不匹配");
            }
        }
    }
 
    /// <summary>
    /// 触发三参数事件
    /// </summary>
    public void Broadcast<T, U, V>(string eventId, T arg1, U arg2, V arg3)
    {
        if (eventDict.TryGetValue(eventId, out Delegate d))
        {
            Action<T, U, V> callback = d as Action<T, U, V>;
            if (callback != null)
            {
                callback(arg1, arg2, arg3);
            }
            else
            {
                Debug.LogError($"事件 {eventId} 的委托类型不匹配");
            }
        }
    }
 
    #endregion
 
    #region 辅助方法
 
    /// <summary>
    /// 添加监听前的处理
    /// </summary>
    private void OnListenerAdding(string eventId, Delegate callback)
    {
        if (!eventDict.ContainsKey(eventId))
        {
            eventDict.Add(eventId, null);
        }
 
        Delegate d = eventDict[eventId];
        if (d != null && d.GetType() != callback.GetType())
        {
            Debug.LogError($"尝试为事件 {eventId} 添加不同类型的委托,当前类型:{d.GetType()},尝试添加类型:{callback.GetType()}");
        }
    }
 
    /// <summary>
    /// 移除监听前的处理
    /// </summary>
    private bool OnListenerRemoving(string eventId, Delegate callback)
    {
        if (!eventDict.ContainsKey(eventId))
        {
            Debug.LogWarning($"尝试移除不存在的事件 {eventId}");
            return false;
        }
 
        Delegate d = eventDict[eventId];
        if (d == null)
        {
            Debug.LogWarning($"尝试移除事件 {eventId} 的空委托");
            return false;
        }
        else if (d.GetType() != callback.GetType())
        {
            Debug.LogError($"尝试为事件 {eventId} 移除不同类型的委托,当前类型:{d.GetType()},尝试移除类型:{callback.GetType()}");
            return false;
        }
 
        return true;
    }
 
    /// <summary>
    /// 移除监听后的处理
    /// </summary>
    private void OnListenerRemoved(string eventId)
    {
        if (eventDict[eventId] == null)
        {
            eventDict.Remove(eventId);
        }
    }
 
    /// <summary>
    /// 清空所有事件
    /// </summary>
    public void ClearAllEvents()
    {
        eventDict.Clear();
    }
 
    #endregion
}