主要是利用委托及事件的监听和广播来达到代码的高度解耦和
以Button的点击事件为例进行简单的探讨:
代码很简单只有短短的几十行代码(先上代码然后根据代码来简单说明)完整的工程可去本人资源下载里面去下载
CallBack:(这个里面封装的是委托事件,跟Unity给我们的预定义委托Action是一样的,你也可以直接使用Action,不过这里自己封装的委托事件的参数的多少你可以自己控制)
public delegate void CallBack();
public delegate void CallBack<T>(T arg);
public delegate void CallBack<T,X>(T arg,X arg1);
public delegate void CallBack<T,X,Y>(T arg,X arg1,Y arg2);
public delegate void CallBack<T,X,Y,Z>(T arg,X arg1,Y arg2,Z arg3);
public delegate void CallBack<T,X,Y,Z,W>(T arg,X arg1,Y arg2, Z arg3,W arg4);
EventType:(这是一个枚举代表的是事件码,简单来说就是一个标识,代码如下,里面的枚举类型可在使用的时候自行添加,这里我暂时添加的是我之后要用到的一个枚举值)
public enum EventType
{
ShowText,
}
EventCenter:(这个是关键点,这个类里面是事件处理,包含了添加监听事件,移除监听事件以及进行事件广播,这里面我目前只写了六个类型的重载,可自行添加 ,这个里面以字典来保存委托事件,其中以事件码为唯一键,进行添加删除以及广播,代码如下:直接上最简代码)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 事件处理中心
/// </summary>
public class EventCenter
{
/// <summary>
/// 字典用来保存委托事件和事件码
/// </summary>
private static Dictionary<EventType, Delegate> m_EventTable = new Dictionary<EventType, Delegate>();
private static void OnListenerAdding(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()));
}
}
private static void OnListenerRemoving(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));
}
}
private static void OnListenerRemoved(EventType eventType)
{
if (m_EventTable[eventType]==null)
{
m_EventTable.Remove(eventType);
}
}
/// <summary>
/// 无参的 监听事件 的方法
/// </summary>
public static void AddListener(EventType eventType,CallBack callBack)
{
OnListenerAdding(eventType,callBack);
m_EventTable[eventType]= (CallBack)m_EventTable[eventType] + callBack;
}
/// <summary>
/// 一个参数 监听事件 的方法
/// </summary>
public static void AddListener<T>(EventType eventType, CallBack<T> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
}
/// <summary>
/// 二个参数 监听事件 的方法
/// </summary>
public static void AddListener<T,X>(EventType eventType, CallBack<T,X> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T,X>)m_EventTable[eventType] + callBack;
}
/// <summary>
/// 三个参数 监听事件 的方法
/// </summary>
public static void AddListener<T,X,Y>(EventType eventType, CallBack<T,X,Y> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T,X,Y>)m_EventTable[eventType] + callBack;
}
/// <summary>
/// 四个参数 监听事件 的方法
/// </summary>
public static void AddListener<T,X,Y,Z>(EventType eventType, CallBack<T,X,Y,Z> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T,X,Y,Z>)m_EventTable[eventType] + callBack;
}
/// <summary>
/// 五个参数 监听事件 的方法
/// </summary>
public static void AddListener<T,X,Y,Z,W>(EventType eventType, CallBack<T,X,Y,Z,W> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T,X,Y,Z,W>)m_EventTable[eventType] + callBack;
}
/// <summary>
/// 无参的移除监听事件 的方法
/// </summary>
public static void RemoveListener(EventType eventType,CallBack callBack)
{
OnListenerRemoving(eventType,callBack);
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 一参的移除监听事件 的方法
/// </summary>
public static void RemoveListener<T>(EventType eventType, CallBack<T> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 二参的移除监听事件 的方法
/// </summary>
public static void RemoveListener<T,X>(EventType eventType, CallBack<T, X> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 三参的移除监听事件 的方法
/// </summary>
public static void RemoveListener<T, X,Y>(EventType eventType, CallBack<T, X, Y> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 四参的移除监听事件 的方法
/// </summary>
public static void RemoveListener<T, X, Y,Z>(EventType eventType, CallBack<T, X, Y, Z> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 五参的移除监听事件 的方法
/// </summary>
public static void RemoveListener<T, X, Y, Z,W>(EventType eventType, CallBack<T, X, Y, Z, W> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 无参的广播监听事件
/// </summary>
/// <param name="eventType"></param>
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//当callBack为空的话就是强转错误,就是说类型对应委托的类型不同
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托有不同的类型",eventType));
}
}
}
/// <summary>
/// 一参的广播监听事件
/// </summary>
/// <param name="eventType"></param>
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//当callBack为空的话就是强转错误,就是说类型对应委托的类型不同
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托有不同的类型", eventType));
}
}
}
/// <summary>
/// 二参的广播监听事件
/// </summary>
/// <param name="eventType"></param>
public static void Broadcast<T,X>(EventType eventType,T arg,X arg1)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T, X> callBack = d as CallBack<T, X>;
if (callBack != null)
{
callBack(arg,arg1);
}
else//当callBack为空的话就是强转错误,就是说类型对应委托的类型不同
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托有不同的类型", eventType));
}
}
}
/// <summary>
/// 三参的广播监听事件
/// </summary>
/// <param name="eventType"></param>
public static void Broadcast<T, X,Y>(EventType eventType,T arg,X arg1, Y arg2)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T, X, Y> callBack = d as CallBack<T, X, Y>;
if (callBack != null)
{
callBack(arg,arg1,arg2);
}
else//当callBack为空的话就是强转错误,就是说类型对应委托的类型不同
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托有不同的类型", eventType));
}
}
}
/// <summary>
/// 四参的广播监听事件
/// </summary>
/// <param name="eventType"></param>
public static void Broadcast<T, X, Y,Z>(EventType eventType,T arg,X arg1,Y arg2,Z arg3)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T, X, Y, Z> callBack = d as CallBack<T, X, Y, Z>;
if (callBack != null)
{
callBack(arg,arg1,arg2,arg3);
}
else//当callBack为空的话就是强转错误,就是说类型对应委托的类型不同
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托有不同的类型", eventType));
}
}
}
/// <summary>
/// 五参的广播监听事件
/// </summary>
/// <param name="eventType"></param>
public static void Broadcast<T, X, Y, Z,W>(EventType eventType,T arg,X arg1,Y arg2, Z arg3,W arg4)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T, X, Y, Z, W> callBack = d as CallBack<T, X, Y, Z, W>;
if (callBack != null)
{
callBack(arg,arg1,arg2,arg3,arg4);
}
else//当callBack为空的话就是强转错误,就是说类型对应委托的类型不同
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托有不同的类型", eventType));
}
}
}
}
关于这些工具型代码的使用(以Button点击事件为例直接上代码讲解):
ShowText:(这个代码挂载到Text场景物体上获取的是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,string,string,int,int>(EventType.ShowText,Show);//添加事件监听
}
private void OnDestroy()
{
EventCenter.RemoveListener<string, string, string, int, int>(EventType.ShowText,Show);//结束的时候移除事件监听
}
private void Show(string a,string b,string c,int d,int e)
{
gameObject.SetActive(true);
GetComponent<Text>().text = a+b+c+d+e;
}
}
BtnOnClick:(这个挂载到场景中的Button按钮上即可)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BtnOnClick : MonoBehaviour
{
private void Awake()
{
GetComponent<Button>().onClick.AddListener(()=> {
EventCenter.Broadcast(EventType.ShowText, "2019事业有成,走遍千山万水","access","优秀",52,1);
});
}
}
这种方法的
优点:高度解耦和,互不影响,监听事件和广播事件少了谁都不会直接报错,这个里面是以事件码作为键用字典这种数据结构来进行使用。
缺点:就是在使用的时候参数的类型顺序必须对应,且无相关提示。
相互讨论学习,如有不同观点或疑惑可留言相互讨论