| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace ProjectBase.Event
- {
- public class EventHandlerItem
- {
- public string eventKey;
- public List<EventInfo> eventInfos = new List<EventInfo>();
- public EventHandler action;
- public EventHandlerItem(string key, EventHandler handler, int time)
- {
- eventKey = key;
- eventInfos.Add(new EventInfo(handler, time));
- action += handler;
- }
- /// <summary>
- /// 添加事件
- /// </summary>
- /// <param name="handler">事件</param>
- /// <param name="time">次数</param>
- public void AddEventHandler(EventHandler handler, int time)
- {
- eventInfos.Add(new EventInfo(handler, time));
- action += handler;
- }
- /// <summary>
- /// 触发事件
- /// </summary>
- /// <param name="info">参数</param>
- public void EventHandlerTrigger(BaseEventData eventData)
- {
- action?.Invoke(eventData);
- for (int i = eventInfos.Count - 1; i >= 0; i--)
- {
- if (eventInfos[i].time == EventCenter.Infinity)
- {
- continue;
- }
- eventInfos[i].time--;
- if (eventInfos[i].time <= 0)
- {
- eventInfos.Remove(eventInfos[i]);
- action -= eventInfos[i].action;
- }
- }
- }
- public void RemoveEventHandler(EventHandler handler)
- {
- for (int i = eventInfos.Count - 1; i >= 0; i--)
- {
- if (eventInfos[i].action == handler)
- {
- eventInfos.Remove(eventInfos[i]);
- action -= handler;
- }
- }
- }
- }
- }
|