blob: 5a1171e101e0cb05b14406e72303b1c575a2bc61 (
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace WK
{
/// <summary>
/// 作用域范围的消息通知
/// </summary>
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);
}
}
}
|