EventSystem 事件系统
事件系统在开发中必不可少事件系统使用观察者模式可以极大程度降低程序的耦合,之前的文章也讲过事件系统但是不够高效简洁,如何轻便高效优雅的实现一个事件呢?依然基于之前的AssemblyManager 程序集管理器和SingletonSystem 单例管理系统进行开发,主要原理也是根据反射去获取事件的接口使用反射进行实例化处理类,而非人工手动订阅事件。文章来源:https://www.toymoban.com/news/detail-702359.html
1.IEvent
public interface IEvent
{
Type GetEventType();
void Invoke(object self);
}
2.EventHandler
public abstract class EventHandler<T> : IEvent where T : struct
{
private readonly Type _selfType = typeof(T);
public Type GetEventType()
{
return _selfType;
}
public abstract void Handler(T eventStruct);
public void Invoke(object self)
{
try
{
Handler((T)self);
}
catch (Exception ex)
{
Debug.LogError($"{_selfType.Name} Error : {ex.Message}");
return;
}
}
}
3.EventInfo
public sealed class EventInfo
{
public readonly Type Type;
public readonly IEvent Event;
public EventInfo(Type type, IEvent @event)
{
Type = type;
Event = @event;
}
}
4.EventSystem
public class EventSystem : Singleton<EventSystem>
{
private readonly OneToManyList<Type, IEvent> _events = new();
private readonly OneToManyList<int, EventInfo> _assemblyEvents = new();
protected override void Load(int assemblyName)
{
foreach (Type type in AssemblyManager.ForEach(assemblyName, typeof(IEvent)))
{
IEvent @event = (IEvent)Activator.CreateInstance(type);
if (@event != null)
{
Type thisType = @event.GetEventType();
_events.Add(thisType, @event);
_assemblyEvents.Add(assemblyName, new EventInfo(thisType, @event));
}
}
}
protected override void UnLoad(int assemblyName)
{
if(_assemblyEvents.TryGetValue(assemblyName,out List<EventInfo> events))
{
foreach (EventInfo info in events)
{
_events.RemoveValue(info.Type, info.Event);
}
_assemblyEvents.RemoveByKey(assemblyName);
}
}
public void Publish<TEventData>(TEventData eventData) where TEventData : struct
{
if (!_events.TryGetValue(eventData.GetType(), out List<IEvent> list))
return;
foreach (IEvent @event in list)
{
try
{
@event?.Invoke(eventData);
}
catch (Exception ex)
{
Debug.LogError(ex.Message);
return;
}
}
}
public override void Dispose()
{
_events.Clear();
_assemblyEvents.Clear();
base.Dispose();
}
}
这里的每个事件传递的参数可以使用引用池进行管理避免重复创建相同的对象消耗性能。文章来源地址https://www.toymoban.com/news/detail-702359.html
5.测试
public class Test : MonoBehaviour
{
void Start()
{
SingletonSystem.Initialize();
AssemblyManager.Initialize();
}
private void Update()
{
SingletonSystem.Update();
if (Input.GetKeyDown(KeyCode.P))
{
EventSystem.Instance.Publish<AgeEvent>(new AgeEvent() { Age = 18 });
}
}
}
public struct AgeEvent
{
public int Age;
}
public class AgeChangeHandler : EventHandler<AgeEvent>
{
public override void Handler(AgeEvent eventStruct)
{
Debug.Log(eventStruct.Age);
}
}
到了这里,关于EventSystem 事件系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!