阿木自学unity系列--第三篇Unity高度解耦和-事件的监听与广播系统----2019.1.31
本来昨天想开始学Carson老师最新的初级课程–幻径小游戏的制作的,可是听了前两个视频才知道有先修课程哈哈哈哈,所以屁颠屁颠的跑去学了今天要记录的这门课–Unity高度解耦和,关于事件的监听与广播系统,这门课程主要就是将怎么用代码实现较方便的事件监听以及广播,需要了解的同学们可以往下看啦。
课程名称:Unity高度解耦和 - 事件的监听与广播系统(Unity2018.1.0)
我的Unity版本:2018.3.0f2
那么,高度解耦和是一个什么东西呢,视频看完了其实我也不是太了解,以我的理解是这样的:就是将事件的监听的每个步骤分成一个独立的个体,互不影响,即使其中一个步骤出错了,程序也不会报错,相反,没有使用高度解耦和的话,万一程序其中一处出错,就像多米诺一样倒一片。总的来说,高度解耦和还是利大于弊的。
简单总结一下高度解耦和系统的优点与缺点
优点:
代码简单,三个代码脚本解决。
解决了代码和代码之间的高度耦合性,每个代码都是独立的,不与外界产生联系。
缺点:
添加监听的时候,监听的方法的类型必须与泛型的顺序一致,不然会报错。
广播的时候,必须知道广播的事件的参数的类型与顺序。
核心代码讲解:
- 第一个脚本代码
//EventType.cs
public enum EventType
{
ShowText,
}
创建一个枚举类型的事件类型,这里命名为ShowText,按实际的事件类型来添加枚举类型
- 第二个脚本代码
//CallBack.cs
public delegate void CallBack();
public delegate void CallBack<T>(T arg);
public delegate void CallBack<T,X>(T arg1,X arg2);
public delegate void CallBack<T, X,Y>(T arg1, X arg2,Y arg3);
public delegate void CallBack<T, X, Y,Z>(T arg1, X arg2, Y arg3,Z arg4);
public delegate void CallBack<T, X, Y, Z,W>(T arg1, X arg2, Y arg3, Z arg4,W arg5);
这里新建一个CallBack类用作委托,包含了一个无参的,5个有参的泛型函数。
- 第三个脚本代码
//EventCenter.cs
using System;//这里记得引入Delegate的命名空间System,不然Delegate引用不了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EventCenter
{
//定义一个静态的字典类型,key为事件码EventType,value为委托事件。
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)//如果事件码为空,则调用Remove方法移除事件码
{
m_EventTable.Remove(eventType);
}
}
//no parameters//无参
public static void AddListener(EventType eventType, CallBack callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
}
//single parameters//与无参大致相同
public static void AddListener<T>(EventType eventType,CallBack<T> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
}
//two parameters//与无参大致相同
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;
}
//three parameters//与无参大致相同
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;
}
//four parameters//与无参大致相同
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;
}
//five parameters//与无参大致相同
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;
}
//no parameters//无参
public static void RemoveListener(EventType eventType, CallBack callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
//single parameters//与无参大致相同
public static void RemoveListener<T>(EventType eventType, CallBack<T> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
//two parameters//与无参大致相同
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);
}
//three parameters//与无参大致相同
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);
}
//four parameters//与无参大致相同
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);
}
//five parameters//与无参大致相同
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);
}
//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 parameters//与无参大致相同
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 parameters//与无参大致相同
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));
}
}
}
//three parameters//与无参大致相同
public static void Broadcast<T, X,Y>(EventType eventType, T arg1, X arg2,Y arg3)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T, X,Y> callBack = d as CallBack<T, X,Y>;
if (callBack != null)
{
callBack(arg1, arg2,arg3);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同的类型", eventType));
}
}
}
//four parameters//与无参大致相同
public static void Broadcast<T, X, Y,Z>(EventType eventType, T arg1, X arg2, Y arg3,Z arg4)
{
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(arg1, arg2, arg3,arg4);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同的类型", eventType));
}
}
}
//five parameters//与无参大致相同
public static void Broadcast<T, X, Y, Z,W>(EventType eventType, T arg1, X arg2, Y arg3, Z arg4,W arg5)
{
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(arg1, arg2, arg3, arg4,arg5);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同的类型", eventType));
}
}
}
}
核心代码就是这三个脚本代码,总的来说,代码还是很简单明了的,不会说很难懂。
接下来就是测试代码
- 按钮点击脚本
//BtnClick.cs
//挂载在按钮Button下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BtnClick : MonoBehaviour
{
private void Awake()
{
GetComponent<Button>().onClick.AddListener(() =>
{
EventCenter.Broadcast(EventType.ShowText,"你好","呀",1.0f,1,2);
});
}
}
- 显示文字脚本
//ShowText.cs
//挂载在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,float,int,int>(EventType.ShowText, Show);
}
private void OnDestroy()
{
EventCenter.RemoveListener<string,string,float,int,int>(EventType.ShowText, Show);
}
public void Show(string str,string str1,float a,int b,int c)
{
gameObject.SetActive(true);
GetComponent<Text>().text = str + str1 + a + b + c;
}
}
实现的功能就是很简单的点击按钮显示文字,截图如下
资源文件(项目源文件及解耦和核心代码)下载地址:
http://file.yiyuen.com/detail.html?id=1382
终于可以睡觉啦~~各位晚安!!!
下期给大家带来超级容易上瘾的小游戏–幻径的制作课程!!
2019/1/31/03:26