源码:
//EventCenter
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class EventCenter
{
static private Dictionary<EventType, Delegate> m_EventTable = new Dictionary<EventType, Delegate>();
private static void AddListenering(EventType eventType, Delegate callBack)
{
if (!m_EventTable.ContainsKey(eventType))
{
m_EventTable.Add(eventType, null);
}
Delegate d = m_EventTable[eventType];
if (d != null && d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托是{1},要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
}
}
//no parameters
public static void AddListener(EventType eventType, CallBack callBack)
{
AddListenering(eventType, callBack);
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
}
//single
public static void AddListener<T>(EventType eventType, CallBack<T> callBack)
{
AddListenering(eventType, callBack);
m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
}
//two
public static void AddListener<T, X>(EventType eventType, CallBack<T, X> callBack)
{
AddListenering(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] + callBack;
}
private static void RomoveListenering(EventType eventType, Delegate callBack)
{
if (m_EventTable.ContainsKey(eventType))
{
Delegate d = m_EventTable[eventType];
if (d == null)
{
throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType));
}
else if (d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前委托类型为{1},要移除的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
}
}
else
{
throw new Exception(string.Format("移除监听错误,没有事件码{0}", eventType));
}
}
//no parameters
public static void RemoveListener(EventType eventType, CallBack callBack)
{
RomoveListenering(eventType, callBack);
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
RemoveListenered(eventType);
}
//single
public static void RemoveListener<T>(EventType eventType, CallBack<T> callBack)
{
RomoveListenering(eventType, callBack);
m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
RemoveListenered(eventType);
}
//two
public static void RemoveListener<T, X>(EventType eventType, CallBack<T,X> callBack)
{
RomoveListenering(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] - callBack;
RemoveListenered(eventType);
}
private static void RemoveListenered(EventType eventType)
{
if (m_EventTable[eventType] == null)
{
m_EventTable.Remove(eventType);
}
}
//no parameters
public static void Broadcast(EventType eventType)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack callBack = d as CallBack;
if (callBack != null)
{
callBack();
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
//single
public static void Broadcast<T>(EventType eventType, T arg)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T> callBack = d as CallBack<T>;
if (callBack != null)
{
callBack(arg);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
//two
public static void Broadcast<T, X>(EventType eventType, T arg1, X arg2)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T, X> callBack = d as CallBack<T, X>;
if (callBack != null)
{
callBack(arg1, arg2);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
}
//EventType
public enum EventType
{
ShowText
}
//事件码,可以自己随意添加,象征意
//CallBack
public delegate void CallBack();
public delegate void CallBack<T>(T arg);
public delegate void CallBack<T, X>(T arg1, X arg2);
//自定义的委托类型,如果需要更多参数可以自己添加,EventCenter中的方法也要记得添加
使用:
//ShowText
//挂在Text物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShowText : MonoBehaviour
{
private void Awake()
{
gameObject.SetActive(false);
EventCenter.AddListener<string>(EventType.ShowText, Show);//只需要提供相应的事件码和委托,若委托的参数存在的话可以按照顺序添加在<>之中,不存在可以去掉<>,添加监听完成
}
private void OnDestroy()
{
EventCenter.RemoveListener<string>(EventType.ShowText, Show);//移除同理
}
private void Show(string s)
{
gameObject.SetActive(true);
GetComponent<Text>().text = s;
}
}
//BtnClick
//挂在Button上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BtnClick : MonoBehaviour
{
private void Update()
{
GetComponent<Button>().onClick.AddListener(() => { EventCenter.Broadcast<string>(EventType.ShowText, "Hello World!"); });
//广播的时候提供事件码和方法的参数(参数按照顺序),参数类型写在<>之中(按顺序),没有可以不写
}
}
优点:大大降低代码之间的耦合性
缺点:参数调用一定要按照预先确定好的顺序