using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
/*
Title:
Description:
*/
public class Eventmanger:MonoSingleton<EventManger>
{
private Dictionary<string, UnityAction> actions = new Dictionary<string, UnityAction>();
//添加一个事件的监听者
public void AddListener(string eventName,UnityAction action)
{
if(actions.ContainsKey(eventName))
{
actions[eventName] += action;
}
else
{
actions.Add(eventName, action);
}
}
//移除一个事件监听者
public void RemoveListener(string eventName,UnityAction action)
{
if(actions.ContainsKey(eventName))
{
actions[eventName] -= action;
}
}
//触发事件无参数
public void TriggerEvent(string eventName)
{
if(actions.ContainsKey(eventName))
{
actions[eventName].Invoke();
}
}
//清空所有事件
public void RemoveAllListener(string eventName)
{
if(actions.ContainsKey(eventName))
{
actions[eventName] = null;
}
}
//清空所有
public void RemoveAll()
{
actions = null;
}
}
Unity中的事件中心管理器
于 2023-05-18 13:32:00 首次发布