Skip to content

Commit 1ef1978

Browse files
committed
基于EventQueue和观察者模式,实现一个轻量级的EventManger
基于EventQueue和观察者模式,实现了一个轻量级的EventManger事件管理,消息分发器
1 parent 5e5d472 commit 1ef1978

5 files changed

Lines changed: 343 additions & 116 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
//-------------------------------------------------------------------------------------
2+
// EventManger.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
9+
namespace EventQueuePatternExample3
10+
{
11+
/// <summary>
12+
/// 消息管理类-单例
13+
/// </summary>
14+
public class EventManger
15+
{
16+
private static EventManger _Instance;
17+
public static EventManger Instance
18+
{
19+
get
20+
{
21+
if (_Instance == null)
22+
{
23+
_Instance = new EventManger();
24+
}
25+
return _Instance;
26+
}
27+
}
28+
29+
public delegate void EventHandler(BaseEventMsg Msg);
30+
31+
// 消息ID,代理存储字典Map - <(int)EventType,EventHandler代理>
32+
private Dictionary<int, EventHandler> m_EventHandlerMap = new Dictionary<int, EventHandler>();
33+
34+
/// <summary>
35+
/// 注册事件
36+
/// </summary>
37+
public void RegisterEvent(EventType eventType, EventHandler eventHandler)
38+
{
39+
40+
if (m_EventHandlerMap == null)
41+
{
42+
m_EventHandlerMap = new Dictionary<int, EventHandler>();
43+
}
44+
int eventTypeID = (int)eventType;
45+
if (m_EventHandlerMap.ContainsKey(eventTypeID))
46+
{
47+
m_EventHandlerMap[eventTypeID] += eventHandler;
48+
}
49+
else
50+
{
51+
m_EventHandlerMap.Add(eventTypeID, eventHandler);
52+
}
53+
54+
}
55+
56+
/// <summary>
57+
/// 反注册事件-反注册该EventType下所有的注册消息
58+
/// </summary>
59+
public void UnRegisterEvent(EventType eventType)
60+
{
61+
int eventTypeID = (int)eventType;
62+
63+
if (m_EventHandlerMap == null)
64+
{
65+
return;
66+
}
67+
68+
if (m_EventHandlerMap.ContainsKey(eventTypeID))
69+
{
70+
m_EventHandlerMap.Remove(eventTypeID);
71+
}
72+
}
73+
74+
/// <summary>
75+
/// 反注册事件-反注册该EventType下所有的注册消息
76+
/// </summary>
77+
public void UnRegisterEvent(EventType eventType, EventHandler eventHandler)
78+
{
79+
int eventTypeID = (int)eventType;
80+
81+
if (m_EventHandlerMap == null)
82+
{
83+
return;
84+
}
85+
//删除eventHandler指定的消息响应
86+
if (m_EventHandlerMap.ContainsKey(eventTypeID))
87+
{
88+
m_EventHandlerMap[eventTypeID] -= eventHandler;
89+
}
90+
}
91+
92+
93+
94+
/// <summary>
95+
/// 从消息ID,代理存储字典Map中找到对应的消息ID绑定的事件消息
96+
/// </summary>
97+
public void SendEvent(EventType eventType, BaseEventMsg Msg)
98+
{
99+
int eventTypeID = (int)eventType;
100+
101+
if (m_EventHandlerMap == null)
102+
{
103+
return;
104+
}
105+
106+
if (m_EventHandlerMap.ContainsKey(eventTypeID))
107+
{
108+
if (m_EventHandlerMap[eventTypeID] != null)
109+
{
110+
m_EventHandlerMap[eventTypeID](Msg);
111+
}
112+
}
113+
}
114+
115+
/// <summary>
116+
/// 发送事件
117+
/// </summary>
118+
public void SendEvent(EventType eventType, params object[] inParams)
119+
{
120+
BaseEventMsg Msg = new BaseEventMsg();
121+
if (Msg != null)
122+
{
123+
Msg.SetEventMsg(eventType, inParams);
124+
SendEvent(eventType, Msg);
125+
}
126+
}
127+
}
128+
129+
130+
131+
/// <summary>
132+
/// 消息内容
133+
/// </summary>
134+
public class BaseEventMsg
135+
{
136+
public EventType MsgType;
137+
public object[] Params = null;
138+
139+
public BaseEventMsg()
140+
{
141+
142+
}
143+
144+
public BaseEventMsg(EventType inMsgType, params object[] InParams)
145+
{
146+
MsgType = inMsgType;
147+
Params = InParams;
148+
}
149+
150+
public void SetEventMsg(EventType inMsgType, params object[] InParams)
151+
{
152+
MsgType = inMsgType;
153+
Params = InParams;
154+
}
155+
156+
}
157+
158+
}

Assets/Game Programming Patterns/Event Queue Pattern/Example3/EventManger.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)