此处借鉴唐老师的事件系统。本人进行了书写规范化及一些非空判断避免报错等小优化。
如果对委托还不太了解的建议先看一下委托
使用方法很简单,通常在Start或者需要的地方录入一下监听事件,再去实际触发事件的地方调用一下事件触发,原监听的地方就可以收到事件:
using System;
using UnityEngine;
public class Test : MonoBehaviour
{
private void Start()
{
//调用方法一:直接输入监听的事件名,事件回调的函数(无参)
EventCenter.Instance.AddEventListener("LeftMouse1", LeftDown);
//调用方法二:Lambda表达式中再去调用有参函数
//本架构仅允许了一个参数的重载,如果有需求多个参数请自行扩写
EventCenter.Instance.AddEventListener("LeftMouse2", (string str) => { LeftDown2(str); });
//调用方法三:新建一个Action,去+需要的监听函数
Action leftAction = LeftDown;
leftAction += LeftDown3;
EventCenter.Instance.AddEventListener("LeftMouse1", leftAction);
}
private void LeftDown()
{
Debug.Log("左键按下1");
}
private void LeftDown2(string name)
{
Debug.Log("左键按下2" + name);
}
private void LeftDown3()
{
Debug.Log("左键按下3");
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
//此处调用触发事件,参数需与录入监听的事件参数相同
EventCenter.Instance.EventTrigger("LeftMouse1");
EventCenter.Instance.EventTrigger("LeftMouse2", "xxxx");
}
if (Input.GetMouseButtonDown(1))
{
EventCenter.Instance.RemoveEventListener("LeftMouse1", LeftDown);
}
}
}
事件系统代码,基于单例:
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 定义一个空接口,作为EventInfo的基类(使用基类存储子类),则字典中存储的为EventInfo类型
/// </summary>
public interface IEventInfo { }
/// <summary>
/// 将Action转化为泛型,包裹在EventInfo,则字典中存储的为EventInfo类型
/// </summary>
/// <typeparam name="T">T</typeparam>
public class EventInfo<T> : IEventInfo
{
public Action<T> actions;
public EventInfo(Action<T> action)
{
actions += action;
}
}
/// <summary>
/// 用于不需要指定泛型的事件使用
/// </summary>
public class EventInfo : IEventInfo
{
public Action actions;
public EventInfo(Action action)
{
actions += action;
}
}
/// <summary>
/// 事件中心
/// </summary>
public class EventCenter : Singleton<EventCenter>
{
//用于储存所有事件的Dictionary
private Dictionary<string, IEventInfo> m_EventDictionary = new Dictionary<string, IEventInfo>();
/// <summary>
/// 添加监听事件
/// </summary>
/// <param name="name"></param>
/// <param name="action"></param>
public void AddEventListener(string name, Action action)
{
if (m_EventDictionary.ContainsKey(name))
{
if (null == (m_EventDictionary[name] as EventInfo))
Debug.LogError("添加了不同参数的委托");
else
(m_EventDictionary[name] as EventInfo).actions += action;
}
else
m_EventDictionary.Add(name, new EventInfo(action));
}
/// <summary>
/// 添加监听事件(有参)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name">事件名</param>
/// <param name="action">带参数的委托</param>
public void AddEventListener<T>(string name, Action<T> action)
{
if (m_EventDictionary.ContainsKey(name))
{
if (null == (m_EventDictionary[name] as EventInfo<T>))
Debug.LogError("添加了不同参数的委托");
else
(m_EventDictionary[name] as EventInfo<T>).actions += action;
}
else
m_EventDictionary.Add(name, new EventInfo<T>(action));
}
/// <summary>
/// 移除监听
/// </summary>
/// <param name="name"></param>
/// <param name="action"></param>
public void RemoveEventListener(string name, Action action)//移除
{
if (m_EventDictionary.ContainsKey(name))
{
(m_EventDictionary[name] as EventInfo).actions -= action;
if (null == m_EventDictionary[name])
m_EventDictionary.Remove(name);
}
}
/// <summary>
/// 移除监听(有参)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name">事件名</param>
/// <param name="action"></param>
public void RemoveEventListener<T>(string name, Action<T> action)
{
if (m_EventDictionary.ContainsKey(name))
{
(m_EventDictionary[name] as EventInfo<T>).actions -= action;
if (null == m_EventDictionary[name])
m_EventDictionary.Remove(name);
}
}
/// <summary>
/// 触发事件
/// </summary>
/// <param name="name"></param>
public void EventTrigger(string name)
{
if (null == (m_EventDictionary[name] as EventInfo))
{
Debug.LogError("调用了不同参数的委托");
}
else if ((m_EventDictionary[name] as EventInfo).actions != null)
(m_EventDictionary[name] as EventInfo).actions.Invoke();
}
/// <summary>
/// 触发事件(有参)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <param name="info"></param>
public void EventTrigger<T>(string name, T info)
{
if (null == (m_EventDictionary[name] as EventInfo<T>))
{
Debug.LogError("调用了不同参数的委托");
}
else if ((m_EventDictionary[name] as EventInfo<T>).actions != null)
(m_EventDictionary[name] as EventInfo<T>).actions.Invoke(info);
}
/// <summary>
/// 清空所有事件
/// </summary>
public void Clear()
{
m_EventDictionary.Clear();
}
/// <summary>
/// 删除事件及其所有监听
/// </summary>
/// <param name="name">事件名</param>
public void DeleteEvent(string name)
{
if (m_EventDictionary.ContainsKey(name))
{
m_EventDictionary.Remove(name);
}
}
}