首先实现的功能很简单,就是点击按钮,文本显示(内心吐槽:这么简单还自定义干嘛啊!!!当然是为了学习QAQ)
第一步:创建一个枚举类型(测试就写一个类型了)
public enum EventType { eventText }第二步:创建delegate类(用做泛型)
public delegate void EventCallBack(); public delegate void EventCallBack<T>(T arg1); public delegate void EventCallBack<T,W>(T arg1,W arg2); public delegate void EventCallBack<T,W,E>(T arg1, W arg2, E arg3); public delegate void EventCallBack<T,W,E,R>(T arg1, W arg2, E arg3, R arg4); public delegate void EventCallBack<T, W, E, R,Y>(T arg1, W arg2, E arg3, R arg4, Y arg5);第三步(重点来了):定义一个事件中心,用来添加消息,移除消息,广播消息
public class EventCenten { private static Dictionary<EventType, Delegate> m_EventTable = new Dictionary<EventType, Delegate>(); //提取相同的代码段,使用Delegate就可以让方法通用(无论函数是否带参) static void AddingListen(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())); } } static void RemovingListen(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, callBack)); } } else { throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType)); } } //移除 static void OnListenRemove(EventType eventType) { if (m_EventTable[eventType] == null) { m_EventTable.Remove(eventType); } } /// <summary> /// 添加事件 /// </summary> /// <param name="eventType"></param> /// <param name="callBack"></param> //无参的添加方法 public static void AddListen(EventType eventType, EventCallBack callBack) { AddingListen(eventType, callBack); //多播,当键相同后会产生多重广播(调用多个事件) m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callBack; } //一个参的添加方法 public static void AddListen<T>(EventType eventType, EventCallBack<T> callBack) { AddingListen(eventType, callBack); //多播,当键相同后会产生多重广播(调用多个事件) m_EventTable[eventType] = (EventCallBack<T>)m_EventTable[eventType] + callBack; } //两个参的添加方法 public static void AddListen<T, W>(EventType eventType, EventCallBack<T, W> callBack) { AddingListen(eventType, callBack); //多播,当键相同后会产生多重广播(调用多个事件) m_EventTable[eventType] = (EventCallBack<T, W>)m_EventTable[eventType] + callBack; } //三个参的添加方法 public static void AddListen<T, W, E>(EventType eventType, EventCallBack<T, W, E> callBack) { AddingListen(eventType, callBack); //多播,当键相同后会产生多重广播(调用多个事件) m_EventTable[eventType] = (EventCallBack<T, W, E>)m_EventTable[eventType] + callBack; } //四个参的添加方法 public static void AddListen<T, W, E, R>(EventType eventType, EventCallBack<T, W, E, R> callBack) { AddingListen(eventType, callBack); //多播,当键相同后会产生多重广播(调用多个事件) m_EventTable[eventType] = (EventCallBack<T, W, E, R>)m_EventTable[eventType] + callBack; } //五个参的添加方法 public static void AddListen<T, W, E, R, Y>(EventType eventType, EventCallBack<T, W, E, R, Y> callBack) { AddingListen(eventType, callBack); //多播,当键相同后会产生多重广播(调用多个事件) m_EventTable[eventType] = (EventCallBack<T, W, E, R, Y>)m_EventTable[eventType] + callBack; } /// <summary> /// 移除事件 /// </summary> /// <param name="eventType"></param> /// <param name="callBack"></param> //无参的移除方法 public static void RemoveListen(EventType eventType, EventCallBack callBack) { AddingListen(eventType, callBack); m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack; OnListenRemove(eventType); } //一个参的移除方法 public static void RemoveListen<T>(EventType eventType, EventCallBack<T> callBack) { AddingListen(eventType, callBack); m_EventTable[eventType] = (EventCallBack<T>)m_EventTable[eventType] - callBack; OnListenRemove(eventType); } //两个参的移除方法 public static void RemoveListen<T, W>(EventType eventType, EventCallBack<T, W> callBack) { AddingListen(eventType, callBack); m_EventTable[eventType] = (EventCallBack<T, W>)m_EventTable[eventType] - callBack; OnListenRemove(eventType); } //三个参的移除方法 public static void RemoveListen<T, W, E>(EventType eventType, EventCallBack<T, W, E> callBack) { AddingListen(eventType, callBack); m_EventTable[eventType] = (EventCallBack<T, W, E>)m_EventTable[eventType] - callBack; OnListenRemove(eventType); } //四个参的移除方法 public static void RemoveListen<T, W, E, R>(EventType eventType, EventCallBack<T, W, E, R> callBack) { AddingListen(eventType, callBack); m_EventTable[eventType] = (EventCallBack<T, W, E, R>)m_EventTable[eventType] - callBack; OnListenRemove(eventType); } //五个参的移除方法 public static void RemoveListen<T, W, E, R, Y>(EventType eventType, EventCallBack<T, W, E, R, Y> callBack) { AddingListen(eventType, callBack); m_EventTable[eventType] = (EventCallBack<T, W, E, R, Y>)m_EventTable[eventType] - callBack; OnListenRemove(eventType); } /// <summary> /// 广播事件 /// </summary> /// <param name="eventType"></param> //无参广播事件 public static void BroadCast(EventType eventType) { Delegate d; if (m_EventTable.TryGetValue(eventType, out d)) { EventCallBack callBack = d as EventCallBack; if (callBack != null) { callBack(); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType)); } } } //一个参广播事件 public static void BroadCast<T>(EventType eventType, T arg) { Delegate d; if (m_EventTable.TryGetValue(eventType, out d)) { EventCallBack<T> callBack = d as EventCallBack<T>; if (callBack != null) { callBack(arg); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType)); } } } //两个参广播事件 public static void BroadCast<T, W>(EventType eventType, T arg1, W arg2) { Delegate d; if (m_EventTable.TryGetValue(eventType, out d)) { EventCallBack<T, W> callBack = d as EventCallBack<T, W>; if (callBack != null) { callBack(arg1, arg2); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType)); } } } //三个参广播事件 public static void BroadCast<T, W, E>(EventType eventType, T arg1, W arg2, E arg3) { Delegate d; if (m_EventTable.TryGetValue(eventType, out d)) { EventCallBack<T, W, E> callBack = d as EventCallBack<T, W, E>; if (callBack != null) { callBack(arg1, arg2, arg3); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType)); } } } //四个参广播事件 public static void BroadCast<T, W, E, R>(EventType eventType, T arg1, W arg2, E arg3, R arg4) { Delegate d; if (m_EventTable.TryGetValue(eventType, out d)) { EventCallBack<T, W, E, R> callBack = d as EventCallBack<T, W, E, R>; if (callBack != null) { callBack(arg1, arg2, arg3, arg4); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType)); } } } //五个参广播事件 public static void BroadCast<T, W, E, R, Y>(EventType eventType, T arg1, W arg2, E arg3, R arg4, Y arg5) { Delegate d; if (m_EventTable.TryGetValue(eventType, out d)) { EventCallBack<T, W, E, R, Y> callBack = d as EventCallBack<T, W, E, R, Y>; if (callBack != null) { callBack(arg1, arg2, arg3, arg4, arg5); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType)); } } } }第四步(开始使用了):挂载到text上
using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UseText : MonoBehaviour { public static UseText _instance; void Awake() { _instance = this; } // Use this for initialization void Start() { gameObject.SetActive(false); EventCenter.AddListen<string, string, int, float, float>(EventType.eventText,show); } // Update is called once per frame void Update() { } void OnDestroy() { EventCenter.RemoveListen<string,string,int,float,float>(EventType.eventText,show); } public void show(string str,string str1,int i,float j,float b) { gameObject.SetActive(true); transform.GetComponent<Text>().text =str+str1+i+j+b; } }第五步:挂载到button上
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ButtonClick : MonoBehaviour { // Use this for initialization void Start() { transform.GetComponent<Button>().onClick.AddListenen(() => { EventCenter.BroadCast(EventType.eventText,"你好","啊",5,2f,0f); }); } }点击按钮弹出消息,功能已经实现,如果有不懂点击下这个网址http://www.sikiedu.com/my/course/304
