YooAsset学习笔记系列
什么是YooAsset:https://github.com/tuyoogame/YooAsset/tree/main
事件管理器EventManager
前言
1.消息包装类PostWrapper
用来封装消息的类,主要用于延迟分发消息
private class PostWrapper
{
// 分发帧
public int PostFrame;
// 消息ID,一个 hashCode
public int EventID;
// 消息内容,IEventMessage 是消息抽象接口
public IEventMessage Message;
// 释放消息,等待垃圾回收
public void OnRelease()
{
PostFrame = 0;
EventID = 0;
Message = null;
}
}
2.事件容器
使用字典保存所有已经注册监听的事件
使用 List 数组保存延迟执行的事件
private static readonly Dictionary<int, List<Action<IEventMessage>>> _listeners = new Dictionary<int, List<Action<IEventMessage>>>(1000);
private static readonly List<PostWrapper> _postWrappers = new List<PostWrapper>(1000);
3.注册和移除事件监听
/// <summary>
/// 添加监听
/// </summary>
public static void AddListener<TEvent>(System.Action<IEventMessage> listener) where TEvent : IEvent

本文详细介绍了YooAsset中的事件管理系统,包括消息包装类PostWrapper的用途、事件容器的数据结构设计、事件监听的注册与移除方法、事件的实时与延迟分发机制以及如何清除所有事件监听。
最低0.47元/天 解锁文章
1901

被折叠的 条评论
为什么被折叠?



