废话少说,先上代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// 通用接口,有没有泛型都能装,适当的时候进行转化
/// </summary>
public interface MyEventInfo
{
}
/// <summary>
/// 如果有返回值,则基于MyEventInfo接口再创建EventInfo
/// </summary>
public class EventInfo<T> : MyEventInfo
{
public UnityAction<T> myAction;
}
public class EventInfo : MyEventInfo
{
public UnityAction myAction;
}
//多参数
public class EventInfo<T, K> : MyEventInfo
{
public UnityAction<T, K> myAction;
}
public class EventManager : SingletonMono<EventManager>
{
public Dictionary<string, MyEventInfo> actionDic = new Dictionary<string, MyEventInfo>();
//添加事件无泛型和有泛型
public void AddEventListener(string name, UnityAction action)
{
if (actionDic.ContainsKey(name))
{
(actionDic[name] as EventInfo).myAction += action;
// Debug.Log("添加了事件" + name);
}
else
{
//Debug.Log("添加了事件" + name);
actionDic.Add(name, new EventInfo() { myAction = action });
Debug.Log("添加了事件" + name + actionDic.ContainsKey(name));
;
}
}
public void AddEventListener<T>(string name, UnityAction<T> action)
{
if (actionDic.ContainsKey(name))
{
(actionDic[name] as EventInfo<T>).myAction += action;
}
else
{
actionDic.Add(name, new EventInfo<T>() { myAction = action });
}
}
public void AddEventListener<T, K>(string name, UnityAction<T, K> action)
{
if (actionDic.ContainsKey(name))
{
(actionDic[name] as EventInfo<T, K>).myAction += action;
}
else
{
actionDic.Add(name, new EventInfo<T, K>() { myAction = action });
}
}
//移除事件
public void RemoveEventLisetener(string name, UnityAction action)
{
if (actionDic.ContainsKey(name))
{
(actionDic[name] as EventInfo).myAction -= action;
}
}
public void RemoveEventLisetener<T>(string name, UnityAction<T> action)
{
if (actionDic.ContainsKey(name))
{
(actionDic[name] as EventInfo<T>).myAction -= action;
}
}
public void RemoveEventLisetener<T, K>(string name, UnityAction<T, K> action)
{
if (actionDic.ContainsKey(name))
{
(actionDic[name] as EventInfo<T, K>).myAction -= action;
}
}
//触发事件
public void TriggerEventLisetener(string name)
{
(actionDic[name] as EventInfo).myAction?.Invoke();
}
public void TriggerEventLisetener<T>(string name, T value)
{
//泛型不支持多个参数,只能重写
Debug.Log("触发了事件" + actionDic.ContainsKey(name));
(actionDic[name] as EventInfo<T>).myAction?.Invoke(value);
Debug.Log("触发事件2");
}
public void TriggerEventLisetener<T, K>(string name, T value, K value2)
{
//重写多参
(actionDic[name] as EventInfo<T, K>).myAction?.Invoke(value, value2);
}
//清空事件
public void Clean()
{
actionDic.Clear();
}
}
核心:c#的Dictionary的使用。
将key为事件名,value为事件对象。
AddEventListener | 传入键值对,若没有key事件,则创建,有则添加 |
RemoveEventLisetener | 传入键值对,若没有key什么都不做,有则删除事件 |
TriggerEventLisetener | 传入键,若存在就触发所有键的事件 |
Clean | 清空 |
测试类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EventTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int a=0;
EventManager.GetInstance().AddEventListener("mouseClick", Test);
EventManager.GetInstance().AddEventListener<int>("mouseClick2", Test2);
}
private void Test()
{
Debug.Log("事件中心测试成功");
}
private void Test2(int a)
{
Debug.Log("事件中心测试成功2");
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
EventManager.GetInstance().TriggerEventLisetener("mouseClick");
EventManager.GetInstance().TriggerEventLisetener<int>("mouseClick2",2);
}
}
}