1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace TFramework
- {
- /// <summary>
- /// 事件管理器
- /// </summary>
- public class EventManager:BaseManager
- {
- private Dictionary<string, TAction> _events = new Dictionary<string, TAction>();
- private Dictionary<Type, TAction<EventBase>> events = new Dictionary<Type, TAction<EventBase>>();
- /// <summary>
- /// 注册事件
- /// </summary>
- /// <param name="type">事件类型</param>
- /// <param name="action">事件</param>
- public void RegistEvent(Type type, TAction<EventBase> action)
- {
- if (events.ContainsKey(type))
- events[type] += action;
- else
- events.Add(type, action);
- }
- /// <summary>
- /// 注册事件
- /// </summary>
- /// <param name="T">事件类型</param>
- /// <param name="action">事件</param>
- public void RegistEvent<T>(TAction<EventBase> action) => RegistEvent(typeof(T), action);
- /// <summary>
- /// 派发事件
- /// </summary>
- /// <param name="eventBase">事件数据</param>
- public void DispatchEvent(Type type, EventBase eventBase)=> events.TryGet(type)?.Invoke(eventBase);
- /// <summary>
- /// 派发事件
- /// </summary>
- /// <param name="T">事件类型</param>
- public void DispatchEvent<T>(EventBase eventBase) => events.TryGet(typeof(T))?.Invoke(eventBase);
- /// <summary>
- /// 注销事件
- /// </summary>
- /// <typeparam name="type">事件类型</typeparam>
- /// <param name="eventBase">事件</param>
- public void UnRegist(Type type,TAction<EventBase> action)
- {
- if (events.ContainsKey(type))
- events[type] -= action;
- }
- /// <summary>
- /// 注销事件
- /// </summary>
- /// <param name="T">事件类型</param>
- public void UnRegis<T>(TAction<EventBase> action) => UnRegist(typeof(T), action);
- }
- }
|