1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- // #if !COMBAT_SERVER
- using System;
- using System.Collections.Generic;
- using Utility;
- namespace Fort23.Core
- {
- #if !COMBAT_SERVER
- /// <summary>
- /// 事件调度器
- /// </summary>
- public class EventManager : Singleton<EventManager>
- {
- public delegate void CustomEventHandler(IEventData e);
- private readonly Dictionary<CustomEventType, CustomEventHandler> _eventHandlers;
- public EventManager()
- {
- _eventHandlers = new Dictionary<CustomEventType, CustomEventHandler>();
- }
- /// <summary>
- /// 注册事件,没有处理重复,需要注意
- /// </summary>
- /// <param name="type"></param>
- /// <param name="handle"></param>
- public void AddEventListener(CustomEventType type, CustomEventHandler handle)
- {
- if (!_eventHandlers.ContainsKey(type))
- {
- _eventHandlers.Add(type, handle);
- }
- else
- {
- _eventHandlers[type] -= handle;
- _eventHandlers[type] += handle;
- }
- }
- /// <summary>
- /// 取消注册
- /// </summary>
- /// <param name="type"></param>
- /// <param name="handle"></param>
- public void RemoveEventListener(CustomEventType type, CustomEventHandler handle)
- {
- if (_eventHandlers.ContainsKey(type))
- {
- _eventHandlers[type] -= handle;
- }
- }
- /// <summary>
- /// 取消注册所有当前类型的事件
- /// </summary>
- /// <param name="type"></param>
- public void RemoveAllEventListener(CustomEventType type)
- {
- if (_eventHandlers.ContainsKey(type))
- {
- _eventHandlers.Remove(type);
- }
- }
- // ReSharper restore Unity.ExpensiveCode
- /// <summary>
- /// 事件触发执行的方法,Public方便外界调用,谁执行,谁给执行细节
- /// </summary>
- public void Dispatch(CustomEventType type, IEventData eventData, bool isRecycle = true)
- {
- if (_eventHandlers.ContainsKey(type))
- {
- if (_eventHandlers[type] != null)
- {
- _eventHandlers[type](eventData);
- }
- }
- if (isRecycle && eventData != null)
- {
- eventData?.Recycle();
- }
- }
- }
- #endif
- }
- // #endif
|