当前位置:网站首页>Unity practical framework (III) event system
Unity practical framework (III) event system
2022-07-20 10:13:00 【p_ hat-trick】
List of articles
Unity Practical framework ( 3、 ... and ) The event system
stay Unity Based on the event registration provided , Implement a more flexible event system .
Event
First , Define a common event base class for all possible events . The base class contains : The originator of the incident (UNITY object )、 How to respond ( Synchronous or asynchronous ) And whether the event is destroyed after processing .
public abstract class Event : IDisposable
{
private GameObject publisher;
private bool responce;
private bool disposeAfterUsed;
}
that , Any type should inherit from Event, It can be used as an event in MessageSystem Release and monitor inside . Of course , If each event has a separate type , It may make the number of classes too large , All events under the same module can be encapsulated into one Event Among the subclasses of , Then perform secondary search through string or enumeration . For example, the following way :
public class NetWorkConnectionEvent : Event{
...
enum Type{
EventType1,
EventType2,
...
}
...
}
If you use this way of writing ,MessageSystem It will be more complicated , For convenience , Let's assume that all events are inherited from Event, Every Event Represents and only represents one event .
IEventListener
A type implements the interface , To show that you are a type that can listen for events . This interface needs to implement the listening function
public interface IEventListener
{
bool OnEvent(Event e);
}
MessageSystem
Subscribe/Unsubscribe
A basic event system should at least be able to publish and subscribe events . First , We define the following lists , To store subscriber information .
private Dictionary<Event, List<IEventListener>> Listeners;
private List<KeyValuePair<Event, IEventListener> subscribeRequests;
private List<KeyValuePair<Event, IEventListener> > unsubscribeRequests
Dictionaries Listeners Stores all listeners registered to an event , The usage is very clear , So what's the use of the following two lists ? Because there will be dynamic event registration and deregistration at any time in the scene , therefore , The following two variables are used in MessageSystem.Update Function , The newly registered Listener Update to Listener in , Or from Listener Remove . When Listener One of the Key Corresponding Value It's empty time ( That is, a certain one. Event Not registered by any listener ), So this KEY Will be removed , Accordingly , And if the newly registered listener finds Listener There is no corresponding Event, A corresponding Key. With this dynamic registration method , It can avoid a large number of useless registration information in the event system to improve operation efficiency .
stay Subscribe/Unsubscribe in , Will need Subscribe/Unsubscribe Listeners of add to the corresponding list .
subscribeRequests.Add(new KeyValuePair<Event, IEventListener>(e, listener);
unsubscribeRequests.Add(new KeyValuePair<Event, IEventListener>(e, listener);
stay Update in , Update them to Listener:
foreach (var subReq in subscribeRequests)
{
List<IEventListener> callbacks = null;
if (Listeners.TryGetValue(subReq.Key, out callbacks))
{
callbacks.Add(subReq.Value);
}
else
{
callbacks = new List<IEventListener>();
callbacks.Add(subReq.Value);
listeners.Add(subReq.Key, callbacks);
}
}
subscribeRequests.Clear();
var infoToUnsubscribe = new SubscribeCallbackInfo(null, false);
foreach (var unsubReq in unsubscribeRequests)
{
List<IEventListener> callbacks = null;
if (Listeners.TryGetValue(unsubReq.Key, out callbacks))
{
if (callbacks.Remove(unsubReq.Value))
{
if (callbacks.Count == 0)
{
subscribeCallbacks.Remove(unsubReq.Key);
}
}
}
}
unsubscribeRequests.Clear();
Publish
Now we have the subscription information , Need one Publish Function to publish events .
public void Publish(Event e);
The publisher decides how the event is handled ( Sync - asynchronous ) And the way of destruction , For asynchronous calls , Direct loop traversal Listener Corresponding List that will do :
List<IEventListener> listeners;
if(Listeners.TryGetValue(e, out listeners)){
for(listener in listeners){
listener.OnEvent(e);
}
}
For synchronous calls , use StartCoroutine Coroutine instead of direct call :
StartCoroutine(listener.OnEvent(e));
Sometimes , We just need the event to be handled by any listener , It doesn't need all listeners to deal with it , therefore , Publishers can put Event.disposeAfterUsed Set as true, stay publish in , As long as a listener handles the event , Then destroy the event , There is no need to pass it down .
List<IEventListener> listeners;
if(Listeners.TryGetValue(e, out listeners)){
for(listener in listeners){
if(listener.OnEvent(e)){
e.dispose();
break;
}
}
}
Update time :2022.7.17
边栏推荐
- 网络安全学习(二十三)防火墙
- 2022-7-8 第八小组 顾宇佳 拓展训练
- 正则表达式的设计
- Experience of installing ROS in Jetson nano (failed)
- 虚幻引擎学习(2)
- FileInputStream与BufferedInputStream有哪些区别?
- 网安学习(二十二)搭建公司和分公司虚拟专线
- 第四次实验nat
- 服务器硬件及RAID配置与实战
- FPGA skimming P4: use 8-3 priority encoder to realize 16-4 priority encoder, use 3-8 decoder to realize full subtracter, realize 3-8 decoder, use 3-8 decoder to realize logic function, and use data se
猜你喜欢
随机推荐
使用google cloud部署基于flask的网站
RISC_V交叉编译环境(国内镜像 避坑)
Unity实用框架(三)事件系统
NJU南京大学高程课设:坦克大战(BattleCity)
Leetcode:20. 有效的括号【三种思路+不同语言实现】
Network Security Learning (21) NAT dynamic routing
【PTA】7-24 约分最简分式 (15 分)
网安学习(二十二)搭建公司和分公司虚拟专线
2022-7-12 第八小组 顾宇佳 (Js)
Qt给控件添加鼠标事件
2022-7-11 Gu Yujia's study notes of group 8 (JS)
浅谈Excel文件解析
在磁盘阵列(RAID)上搭建LVM
FPGA decoder + decoder (including code)
FPGA skimming P4: use 8-3 priority encoder to realize 16-4 priority encoder, use 3-8 decoder to realize full subtracter, realize 3-8 decoder, use 3-8 decoder to realize logic function, and use data se
计算机408+数据库【适合考研复试或期末复习】
Apprentissage de la sécurité des réseaux (XVII) VLAN
黑马程序员多线程实现方式三
雷达基础系列文章之五:雷达调制样式的功能
乐山师范程序设计大赛2020-C: 最大乘积【思维】