方法调用
//发送事件 【发送事件码,发送消息内容】
EventCenterUtil.Broadcast(EventCenterUtil.EventType.Joystick, ui);
//监听无参事件
EventCenterUtil.AddListener(EventCenterUtil.EventType.Joystick, show);
public void show(){}
//发送事件 有参事件
EventCenterUtil.Broadcast(EventCenterUtil.EventType.Joystick, ui, "你好");
EventCenterUtil.AddListener(EventCenterUtil.EventType.Joystick, show);
public void show(string i)
{ }
unity工具类
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Unity.VisualScripting.FlowStateWidget;
public class EventCenterUtil
{
public enum EventType
{
Joystick
}
public delegate void CallBack();
public delegate void CallBack(T arg);
public delegate void CallBack(T arg1, X arg2);
private static Dictionary m_EventTable = new Dictionary();
/**
* 添加监听
* **/
public static void AddListener(EventType eventType, CallBack 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()));
}
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
}
public static void AddListener(EventType eventType, CallBack 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()));
}
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
}
public static void AddListener(EventType eventType, CallBack 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()));
}
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
}
/**
* 移除监听
* **/
public static void RemoveListener(EventType eventType, CallBack 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));
}
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
}
public static void RemoveListener(EventType eventType, CallBack 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));
}
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
}
public static void RemoveListener(EventType eventType, CallBack 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));
}
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
}
/**
* 广播监听
* **/
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));
}
}
}
public static void Broadcast(EventType eventType, T arg)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack callBack = d as CallBack;
if (callBack != null)
{
callBack(arg);
}
else
{
throw new Exception(string.Format("广播事件错误: 事件{0}对应委托具有不同的类型", eventType));
}
}
}
public static void Broadcast(EventType eventType, T arg1, X arg2)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack callBack = d as CallBack;
if (callBack != null)
{
callBack(arg1, arg2);
}
else
{
throw new Exception(string.Format("广播事件错误: 事件{0}对应委托具有不同的类型", eventType));
}
}
}
}