-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventBox.cs
More file actions
240 lines (222 loc) · 7.31 KB
/
EventBox.cs
File metadata and controls
240 lines (222 loc) · 7.31 KB
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
//----------------------------------------------
// C# 简化全局事件发送/接收类
// @author: ChenXing
// @email: onechenxing@163.com
// @date: 2014/12/31
//----------------------------------------------
/*
使用方式:
1.定义事件类型(可以直接定义在发送者类,或者单独创建的事件类)
(注意事件字符串名字中前缀加入类名,防止事件命名冲突,
如果有携带data的注释中注明数据类型,便于接收者解析)
public class MyEventBoxType
{
/// <summary>
/// 测试事件
/// data为int型数据
/// </summary>
public const string EVENT_TEST = "MyEventBoxType_EVENT_TEST";
}
2.发送事件: EventBox.Send(MyEventBoxType.EVENT_TEST, 1);
3.接收事件: EventBox.Add(MyEventBoxType.EVENT_TEST, OnEventTestHandler);
private void OnEventTestHandler(object eventData)
{
Debug.Log("[EVENT_TEST] data:" + eventData);
}
4.移除事件: EventBox.Remove(MyEventBoxType.EVENT_TEST, OnEventTestHandler);
5.移除对象上的所有事件监听: EventBox.RemoveAll(this);
6.清除系统内所有事件监听: EventBox.Dispose();
*/
using System;
using System.Collections.Generic;
/// <summary>
/// 全局事件类
/// </summary>
public class EventBox
{
/// <summary>
/// 事件监听函数代理
/// </summary>
/// <param name="eventData">携带数据</param>
public delegate void EventBoxHandler(object eventData);
/// <summary>
/// 事件监听记录表
/// </summary>
private static Dictionary<string, List<EventBoxHandler>> _listenerDic = new Dictionary<string, List<EventBoxHandler>>();
/// <summary>
/// 对象关联的事件监听记录表
/// </summary>
private static Dictionary<object, ObjectListenerMap> _objListenerDic = new Dictionary<object, ObjectListenerMap>();
/// <summary>
/// 发送事件
/// </summary>
/// <param name="type">事件类型</param>
/// <param name="data">携带数据</param>
public static void Send(string type, object data = null)
{
if (_listenerDic.ContainsKey(type))
{
var handlerList = _listenerDic[type];
for (var i = 0; i < handlerList.Count; i++)
{
handlerList[i](data);
}
}
}
/// <summary>
/// 添加事件监听
/// </summary>
/// <param name="type">事件类型</param>
/// <param name="handler">事件监听回调函数</param>
public static void Add(string type, EventBoxHandler handler)
{
//_listenerDic
if (_listenerDic.ContainsKey(type) == false)
{
List<EventBoxHandler> handlerList = new List<EventBoxHandler> { handler };
_listenerDic.Add(type, handlerList);
}
else
{
if (_listenerDic[type].Contains(handler) == false)
{
_listenerDic[type].Add(handler);
}
}
//_objListenerDic
if (handler.Target != null)
{
if (_objListenerDic.ContainsKey(handler.Target) == false)
{
ObjectListenerMap map = new ObjectListenerMap();
map.Add(type, handler);
_objListenerDic.Add(handler.Target, map);
}
else
{
_objListenerDic[handler.Target].Add(type, handler);
}
}
}
/// <summary>
/// 移除事件监听
/// </summary>
/// <param name="type">事件类型</param>
/// <param name="handler">事件监听回调函数</param>
public static void Remove(string type, EventBoxHandler handler)
{
//_listenerDic
if (_listenerDic.ContainsKey(type))
{
if (_listenerDic[type].Remove(handler))
{
if (_listenerDic[type].Count == 0)
{
_listenerDic.Remove(type);
}
}
}
//_objListenerDic
if (_objListenerDic.ContainsKey(handler.Target))
{
_objListenerDic[handler.Target].Remove(type, handler);
if(_objListenerDic[handler.Target].Get().Count == 0)
{
_objListenerDic.Remove(handler.Target);
}
}
}
/// <summary>
/// 移除某个对象上的所有事件监听
/// </summary>
/// <param name="listener">监听者对象</param>
public static void RemoveAll(object listener)
{
if (_objListenerDic.ContainsKey(listener))
{
foreach (var item in _objListenerDic[listener].Get())
{
//_listenerDic
if (_listenerDic.ContainsKey(item.Key))
{
if (_listenerDic[item.Key].Remove(item.Value))
{
if (_listenerDic[item.Key].Count == 0)
{
_listenerDic.Remove(item.Key);
}
}
}
}
}
//_objListenerDic
_objListenerDic.Remove(listener);
}
/// <summary>
/// 释放系统内所有监听
/// </summary>
public static void Dispose()
{
_listenerDic.Clear();
_objListenerDic.Clear();
}
/// <summary>
/// 打印内部消息列表
/// </summary>
/// <returns></returns>
public static string Print()
{
string txt = "【所有监听类型】\n";
foreach(var item in _listenerDic)
{
txt += string.Format("-->类型:{0},监听数量:{1}\n", item.Key, item.Value.Count);
}
txt += "\n【所有监听对象】\n";
foreach(var map in _objListenerDic)
{
txt += string.Format("-->对象:{0},监听数量:{1}\n", map.Key, map.Value.Get().Count);
}
return txt;
}
}
/// <summary>
/// object事件引用关系内部map类
/// </summary>
class ObjectListenerMap
{
/// <summary>
/// 关联列表(每条记录代表在对象上的Add的一个监听)
/// </summary>
private List<KeyValuePair<string, EventBox.EventBoxHandler>> _mapList = new List<KeyValuePair<string, EventBox.EventBoxHandler>>();
/// <summary>
/// 添加一个监听记录
/// </summary>
/// <param name="type"></param>
/// <param name="handler"></param>
public void Add(string type, EventBox.EventBoxHandler handler)
{
KeyValuePair<string, EventBox.EventBoxHandler> item = new KeyValuePair<string, EventBox.EventBoxHandler>(type, handler);
if(_mapList.Contains(item) == false)
{
_mapList.Add(item);
}
}
/// <summary>
/// 移除一个监听记录
/// </summary>
/// <param name="type"></param>
/// <param name="handler"></param>
public void Remove(string type, EventBox.EventBoxHandler handler)
{
KeyValuePair<string, EventBox.EventBoxHandler> item = new KeyValuePair<string, EventBox.EventBoxHandler>(type, handler);
_mapList.Remove(item);
}
/// <summary>
/// 获得所有监听列表
/// </summary>
/// <returns></returns>
public List<KeyValuePair<string, EventBox.EventBoxHandler>> Get()
{
return _mapList;
}
}