71 lines
1.8 KiB
C#
71 lines
1.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
//消息管理器
|
|
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;
|
|
}
|
|
|
|
public void Dispatch(string eventName)
|
|
{
|
|
if (!_actionDic.ContainsKey(eventName))
|
|
return;
|
|
|
|
_actionDic[eventName]?.Invoke();
|
|
}
|
|
|
|
//带参数的事件
|
|
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);
|
|
}
|
|
} |