/**
* Description : 委托定义
*
* Author : Ankh
*
* CreateTime : 2021-11-13
*
*/
namespace Kernal
{
public delegate void CallBack();
public delegate void CallBack<T>(T arg1);
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);
}
/**
* Description : 事件管理中心
*
* Author : Ankh
*
* CreateTime : 2021-11-13
*
*/
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Kernal
{
public class EventCenter
{
// 存放事件列表
private static Dictionary<EventType, Delegate> m_EventTable = new Dictionary<EventType, Delegate>();
#region 判断方法
// 添加 判断的方法 字典是否已经注册的了相同 事件码的 回调函数
private static bool OnAddListenering(EventType eventType, Delegate callBack)
{
bool isOk = true;
if (!m_EventTable.ContainsKey(eventType))
{
m_EventTable.Add(eventType, null);
}
Delegate d = m_EventTable[eventType];
if (d != null)
{
foreach (Delegate del in d.GetInvocationList())
{
if (object.Equals(del, callBack))
{
isOk = false;
Debug.LogError(string.Format("尝试为事件 {0} 重复添加同一个脚本函数, 要添加的函数名称:{1}()", eventType, callBack.Method.Name));
break;
}
}
if (d.GetType() != callBack.GetType())
{
isOk = false;
Debug.LogError(string.Format("尝试为事件{0}添加不同类型的委托,当前事件对应的委托是{1},要添加的委托类型是{2}", eventType, d.GetType(), callBack.GetType()));
}
}
return isOk;
}
// 添加 移除的方法 字典是否 存在
private static bool OnRemoveListenering(EventType eventType, Delegate callBack)
{
bool isOK = true;
if (m_EventTable.ContainsKey(eventType))
{
Delegate d = m_EventTable[eventType];
if (d == null)
{
Debug.LogError(string.Format("移除监听错误,事件{0}没有对应的委托", eventType));
isOK = false;
}
else if (d.GetType() != callBack.GetType())
{
Debug.LogError(string.Format("移除监听错误,尝试为事件{0}移除不同类型的委托,当前事件对应的委托是{1},要添加的委托类型是{2}", eventType, d.GetType(), callBack.GetType()));
isOK = false;
}
}
else
{
Debug.LogError(string.Format("移除监听错误,没有事件码{0}", eventType));
isOK = false;
}
return isOK;
}
// 添加 移除的方法后需要把对应的事件码移除掉
private static void OnRemovedListenering(EventType eventType)
{
if (m_EventTable[eventType] == null)
{
m_EventTable.Remove(eventType);
}
}
// 判断是否有该类型的事件
private static bool OnBroadcasting(EventType eventType)
{
if (!m_EventTable.ContainsKey(eventType))
{
Debug.Log(string.Format("<color=#FF000>广播事件错误, {0} 类型事件为空 </color>", eventType));
return false;
}
else return true;
}
#endregion
#region 无参数
public static void AddListener(EventType eventType, CallBack callBack)
{
if (OnAddListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
}
public static void RemoveListener(EventType eventType, CallBack callBack)
{
if (OnRemoveListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
OnRemovedListenering(eventType);
}
public static void Broadcast(EventType eventType)
{
if (OnBroadcasting(eventType) == false) return;
CallBack callBack = m_EventTable[eventType] as CallBack;
if (callBack != null)
callBack();
else
Debug.LogError(string.Format("广播事件错误, 该类型为空", eventType));
}
#endregion
#region 一个参数
public static void AddListener<T>(EventType eventType, CallBack<T> callBack)
{
if (OnAddListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
}
public static void RemoveListener<T>(EventType eventType, CallBack<T> callBack)
{
if (OnRemoveListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
OnRemovedListenering(eventType);
}
public static void Broadcast<T>(EventType eventType, T arg)
{
if (OnBroadcasting(eventType) == false) return;
CallBack<T> callBack = m_EventTable[eventType] as CallBack<T>;
if (callBack != null)
callBack(arg);
else
Debug.LogError(string.Format("广播事件错误, 事件{0}为空", eventType));
}
#endregion
#region 两个参数
public static void AddListener<T, X>(EventType eventType, CallBack<T, X> callBack)
{
if (OnAddListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] + callBack;
}
public static void RemoveListener<T, X>(EventType eventType, CallBack<T, X> callBack)
{
if (OnRemoveListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] - callBack;
OnRemovedListenering(eventType);
}
public static void Broadcast<T, X>(EventType eventType, T arg1, X arg2)
{
if (OnBroadcasting(eventType) == false) return;
CallBack<T, X> callBack = m_EventTable[eventType] as CallBack<T, X>;
if (callBack != null)
callBack(arg1, arg2);
else
Debug.LogError(string.Format("广播事件错误, 事件{0}为空", eventType));
}
#endregion
#region 三个参数
public static void AddListener<T, X, Y>(EventType eventType, CallBack<T, X, Y> callBack)
{
if (OnAddListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] + callBack;
}
public static void RemoveListener<T, X, Y>(EventType eventType, CallBack<T, X, Y> callBack)
{
if (OnRemoveListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] - callBack;
OnRemovedListenering(eventType);
}
public static void Broadcast<T, X, Y>(EventType eventType, T arg1, X arg2, Y arg3)
{
if (OnBroadcasting(eventType) == false) return;
CallBack<T, X, Y> callBack = m_EventTable[eventType] as CallBack<T, X, Y>;
if (callBack != null)
callBack(arg1, arg2, arg3);
else
Debug.LogError(string.Format("广播事件错误, 事件{0}为空", eventType));
}
#endregion
#region 四个参数
public static void AddListener<T, X, Y, Z>(EventType eventType, CallBack<T, X, Y, Z> callBack)
{
if (OnAddListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] + callBack;
}
public static void RemoveListener<T, X, Y, Z>(EventType eventType, CallBack<T, X, Y, Z> callBack)
{
if (OnRemoveListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] - callBack;
OnRemovedListenering(eventType);
}
public static void Broadcast<T, X, Y, Z>(EventType eventType, T arg1, X arg2, Y arg3, Z arg4)
{
if (OnBroadcasting(eventType) == false) return;
CallBack<T, X, Y, Z> callBack = m_EventTable[eventType] as CallBack<T, X, Y, Z>;
if (callBack != null)
callBack(arg1, arg2, arg3, arg4);
else
Debug.LogError(string.Format("广播事件错误, 事件{0}为空", eventType));
}
#endregion
#region 五个参数
public static void AddListener<T, X, Y, Z, W>(EventType eventType, CallBack<T, X, Y, Z, W> callBack)
{
if (OnAddListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] + callBack;
}
public static void RemoveListener<T, X, Y, Z, W>(EventType eventType, CallBack<T, X, Y, Z, W> callBack)
{
if (OnRemoveListenering(eventType, callBack) == false) return;
m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] - callBack;
OnRemovedListenering(eventType);
}
public static void Broadcast<T, X, Y, Z, W>(EventType eventType, T arg1, X arg2, Y arg3, Z arg4, W arg5)
{
if (OnBroadcasting(eventType) == false) return;
CallBack<T, X, Y, Z, W> callBack = m_EventTable[eventType] as CallBack<T, X, Y, Z, W>;
if (callBack != null)
callBack(arg1, arg2, arg3, arg4, arg5);
else
Debug.LogError(string.Format("广播事件错误, 事件{0}为空", eventType));
}
#endregion
}
}
/**
* Description : 事件码
*
* Author : Ankh
*
* CreateTime : 2021-11-13
*
*/
namespace Kernal
{
public enum EventType
{
}
}