Health/Assets/Scripts/EventManager.cs

72 lines
1.8 KiB
C#
Raw Normal View History

2023-11-07 13:55:35 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Search;
using UnityEngine;
//<2F><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public class EventManager : MonoSingleton<EventManager>
{
public delegate void EventHandler(params object[] args);
private Dictionary<string, Action> _actionDic = new Dictionary<string, Action>();
private Dictionary<string, EventHandler> _actionsDic = new Dictionary<string, EventHandler>();
public void AddEventListener(string eventName, Action handler)
{
if (!_actionDic.ContainsKey(eventName))
{
_actionDic.Add(eventName, handler);
}
else
{
_actionDic[eventName] -= handler;
_actionDic[eventName] += handler;
}
}
public void RemoveEventListener(string eventName, Action handler)
{
if (!_actionDic.ContainsKey(eventName))
return;
_actionDic[eventName] -= handler;
}
2023-11-07 15:28:19 +00:00
public void Dispatch(string eventName)
2023-11-07 13:55:35 +00:00
{
if (!_actionDic.ContainsKey(eventName))
return;
_actionDic[eventName]?.Invoke();
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
public void AddEventListener(string eventName, EventHandler handler)
{
if (!_actionsDic.ContainsKey(eventName))
{
_actionsDic.Add(eventName, handler);
}
else
{
_actionsDic[eventName] -= handler;
_actionsDic[eventName] += handler;
}
}
public void RemoveEventListener(string eventName, EventHandler handler)
{
if (!_actionsDic.ContainsKey(eventName))
return;
_actionsDic[eventName] -= handler;
}
public void Dispatch(string eventName, params object[] param)
{
if (!_actionsDic.ContainsKey(eventName))
return;
_actionsDic[eventName]?.Invoke(param);
}
}