TestCodeStructure/Assets/Scripts/Project/ProjectBase/EventCenter/EventCenter.cs

265 lines
7.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
public interface IEventInfo
{
}
public class EventInfo : IEventInfo
{
public UnityAction actions;
public EventInfo(UnityAction action)
{
actions += action;
}
}
public class EventInfo<T> : IEventInfo
{
public UnityAction<T> actions;
public EventInfo(UnityAction<T> action)
{
actions += action;
}
}
public class EventInfo<T, D> : IEventInfo
{
public UnityAction<T, D> actions;
public EventInfo(UnityAction<T, D> action)
{
actions += action;
}
}
/// <summary>
/// 事件中心
/// </summary>
public class EventCenter
{
public EventCenter() { }
/// <summary>
/// 存储所有事件 及 关心事件的函数
/// </summary>
// private Dictionary<string, IEventInfo> eventDic = new Dictionary<string, IEventInfo>();
/// <summary>
/// 存储所有事件 及 关心事件的函数
/// </summary>
private Dictionary<Enum_EventType, IEventInfo> eventEnumDic = new Dictionary<Enum_EventType, IEventInfo>();
/// <summary>
/// 添加事件监听
/// </summary>
/// <param name="dic">事件字典</param>
/// <param name="key">事件名称</param>
/// <param name="action">委托函数</param>
private void AddEventListener<T>(Dictionary<T, IEventInfo> dic, T key, UnityAction action)
{
if (dic.ContainsKey(key))
(dic[key] as EventInfo).actions += action;
else
dic.Add(key, new EventInfo(action));
}
/// <summary>
///添加事件监听
/// </summary>
/// <param name="dic">事件字典</param>
/// <param name="key">事件名称</param>
/// <param name="action">委托函数</param>
private void AddEventListener<T, U>(Dictionary<T, IEventInfo> dic, T key, UnityAction<U> action)
{
if (dic.ContainsKey(key))
(dic[key] as EventInfo<U>).actions += action;
else
dic.Add(key, new EventInfo<U>(action));
}
/// <summary>
/// 添加事件监听
/// </summary>
/// <param name="dic">事件字典</param>
/// <param name="key">事件名称</param>
/// <param name="action">委托函数</param>
private void AddEventListener<T, U, V>(Dictionary<T, IEventInfo> dic, T key, UnityAction<U, V> action)
{
if (dic.ContainsKey(key))
(dic[key] as EventInfo<U, V>).actions += action;
else
dic.Add(key, new EventInfo<U, V>(action));
}
/// <summary>
/// 移除事件监听
/// </summary>
/// <param name="dic">事件字典</param>
/// <param name="key">事件名称</param>
/// <param name="action">委托函数</param>
private void RemoveEventListener<T>(Dictionary<T, IEventInfo> dic, T key, UnityAction action)
{
if (dic.ContainsKey(key))
(dic[key] as EventInfo).actions -= action;
}
/// <summary>
/// 移除事件监听
/// </summary>
/// <param name="dic">事件字典</param>
/// <param name="key">事件名称</param>
/// <param name="action">委托函数</param>
private void RemoveEventListener<T, U>(Dictionary<T, IEventInfo> dic, T key, UnityAction<U> action)
{
if (dic.ContainsKey(key))
(dic[key] as EventInfo<U>).actions -= action;
}
/// <summary>
/// 移除事件监听
/// </summary>
/// <param name="dic">事件字典</param>
/// <param name="key">事件名称</param>
/// <param name="action">委托函数</param>
private void RemoveEventListener<T, U, V>(Dictionary<T, IEventInfo> dic, T key, UnityAction<U, V> action)
{
if (dic.ContainsKey(key))
(dic[key] as EventInfo<U, V>).actions -= action;
}
/// <summary>
/// 触发事件
/// </summary>
/// <param name="dic">事件字典</param>
/// <param name="key">事件名称</param>
private void EventTrigger<T>(Dictionary<T, IEventInfo> dic, T key)
{
if (dic.ContainsKey(key))
(dic[key] as EventInfo).actions?.Invoke();
}
/// <summary>
/// 触发事件
/// </summary>
/// <param name="dic">事件字典</param>
/// <param name="key">事件名称</param>
private void EventTrigger<T, U>(Dictionary<T, IEventInfo> dic, T key, U info)
{
if (dic.ContainsKey(key))
(dic[key] as EventInfo<U>).actions?.Invoke(info);
}
/// <summary>
/// 触发事件
/// </summary>
/// <param name="dic">事件字典</param>
/// <param name="key">事件名称</param>
private void EventTrigger<T, U, V>(Dictionary<T, IEventInfo> dic, T key, U info, V info2)
{
if (dic.ContainsKey(key))
(dic[key] as EventInfo<U, V>).actions?.Invoke(info, info2);
}
/// <summary>
/// 添加无参事件监听
/// </summary>
/// <param name="eventType"></param>
/// <param name="action"></param>
public void AddEventListener(Enum_EventType eventType, UnityAction action)
{
AddEventListener(eventEnumDic, eventType, action);
}
/// <summary>
///添加有参事件监听
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="eventType"></param>
/// <param name="action"></param>
public void AddEventListener<T>(Enum_EventType eventType, UnityAction<T> action)
{
AddEventListener(eventEnumDic, eventType, action);
}
/// <summary>
/// 添加多个参数事件监听
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="D"></typeparam>
/// <param name="eventType"></param>
/// <param name="action"></param>
public void AddEventListener<T, D>(Enum_EventType eventType, UnityAction<T, D> action)
{
AddEventListener(eventEnumDic, eventType, action);
}
/// <summary>
/// 移除无参事件监听
/// </summary>
/// <param name="eventType"></param>
/// <param name="action"></param>
public void RemoveEventListener(Enum_EventType eventType, UnityAction action)
{
RemoveEventListener(eventEnumDic, eventType, action);
}
/// <summary>
/// 移除有参事件监听
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="eventType"></param>
/// <param name="action"></param>
public void RemoveEventListener<T>(Enum_EventType eventType, UnityAction<T> action)
{
RemoveEventListener(eventEnumDic, eventType, action);
}
/// <summary>
/// 移除多个参数事件监听
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="D"></typeparam>
/// <param name="eventType"></param>
/// <param name="action"></param>
public void RemoveEventListener<T, D>(Enum_EventType eventType, UnityAction<T, D> action)
{
RemoveEventListener(eventEnumDic, eventType, action);
}
/// <summary>
/// 触发无参事件
/// </summary>
/// <param name="eventType"></param>
public void EventTrigger(Enum_EventType eventType)
{
EventTrigger(eventEnumDic, eventType);
}
/// <summary>
/// 触发有参事件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="eventType"></param>
/// <param name="info"></param>
public void EventTrigger<T>(Enum_EventType eventType, T info)
{
EventTrigger(eventEnumDic, eventType, info);
}
/// <summary>
/// 触发多个参数事件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="D"></typeparam>
/// <param name="eventType"></param>
/// <param name="info"></param>
/// <param name="info2"></param>
public void EventTrigger<T, D>(Enum_EventType eventType, T info, D info2)
{
EventTrigger(eventEnumDic, eventType, info, info2);
}
/// <summary>
/// 清楚所有事件
/// </summary>
public void ClearAllEvent()
{
eventEnumDic.Clear();
}
}