260 lines
7.8 KiB
C#
260 lines
7.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BigSpace.XRCore.Event
|
|
{
|
|
public class GlobalEventMgr
|
|
{
|
|
private static Dictionary<System.Enum, List<Delegate>> events = new Dictionary<Enum, List<Delegate>>();
|
|
|
|
/// <summary>
|
|
/// 添加监听事件
|
|
/// </summary>
|
|
/// <param name="type">监听事件</param>
|
|
/// <param name="listener">监听回调</param>
|
|
public static void Listen(System.Enum type, Action action)
|
|
{
|
|
List<Delegate> listeners;
|
|
if (!events.TryGetValue(type, out listeners))
|
|
{
|
|
listeners = new List<Delegate>();
|
|
events.Add(type, listeners);
|
|
}
|
|
listeners.Add(action);
|
|
}
|
|
/// <summary>
|
|
/// 带一个参数的监听
|
|
/// 用泛型避免使用 params object[] args 带来装箱和拆箱的性能损耗
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="type"></param>
|
|
/// <param name="action"></param>
|
|
public static void Listen<T>(System.Enum type, Action<T> action)
|
|
{
|
|
List<Delegate> listeners;
|
|
if (!events.TryGetValue(type, out listeners))
|
|
{
|
|
listeners = new List<Delegate>();
|
|
events.Add(type, listeners);
|
|
}
|
|
listeners.Add(action);
|
|
|
|
}
|
|
|
|
//两个参数监听回调
|
|
public static void Listen<T, U>(System.Enum type, Action<T, U> action)
|
|
{
|
|
List<Delegate> listeners;
|
|
if (!events.TryGetValue(type, out listeners))
|
|
{
|
|
listeners = new List<Delegate>();
|
|
events.Add(type, listeners);
|
|
}
|
|
listeners.Add(action);
|
|
}
|
|
|
|
//三个参数监听回调
|
|
public static void Listen<T, U, V>(System.Enum type, Action<T, U, V> action)
|
|
{
|
|
List<Delegate> listeners;
|
|
if (!events.TryGetValue(type, out listeners))
|
|
{
|
|
listeners = new List<Delegate>();
|
|
events.Add(type, listeners);
|
|
}
|
|
listeners.Add(action);
|
|
}
|
|
|
|
//四个参数监听回调
|
|
public static void Listen<T, U, V, Z>(System.Enum type, Action<T, U, V, Z> action)
|
|
{
|
|
List<Delegate> listeners;
|
|
if (!events.TryGetValue(type, out listeners))
|
|
{
|
|
listeners = new List<Delegate>();
|
|
events.Add(type, listeners);
|
|
}
|
|
listeners.Add(action);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除所有监听
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
public static void Remove(System.Enum type)
|
|
{
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
events.Remove(type);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除指定的监听
|
|
/// </summary>
|
|
public static void Remove(System.Enum type, Action action)
|
|
{
|
|
//移除指定的监听
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
if (listeners.Contains(action))
|
|
{
|
|
listeners.Remove(action);
|
|
}
|
|
if (listeners.Count <= 0)
|
|
{
|
|
Remove(type);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Remove<T>(System.Enum type, Action<T> action)
|
|
{
|
|
//移除指定的监听
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
if (listeners.Contains(action))
|
|
{
|
|
listeners.Remove(action);
|
|
}
|
|
if (listeners.Count <= 0)
|
|
{
|
|
Remove(type);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Remove<T, U>(System.Enum type, Action<T, U> action)
|
|
{
|
|
//移除指定的监听
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
if (listeners.Contains(action))
|
|
{
|
|
listeners.Remove(action);
|
|
}
|
|
if (listeners.Count <= 0)
|
|
{
|
|
Remove(type);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Remove<T, U, V>(System.Enum type, Action<T, U, V> action)
|
|
{
|
|
//移除指定的监听
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
if (listeners.Contains(action))
|
|
{
|
|
listeners.Remove(action);
|
|
}
|
|
if (listeners.Count <= 0)
|
|
{
|
|
Remove(type);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Remove<T, U, V, Z>(System.Enum type, Action<T, U, V, Z> action)
|
|
{
|
|
//移除指定的监听
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
if (listeners.Contains(action))
|
|
{
|
|
listeners.Remove(action);
|
|
}
|
|
if (listeners.Count <= 0)
|
|
{
|
|
Remove(type);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 调用监听事件
|
|
/// </summary>
|
|
|
|
public static void Dispatch(System.Enum type)
|
|
{
|
|
//Debug.Log($"globalevent0 type: {type}");
|
|
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
for (var i = listeners.Count - 1; i >= 0; i--)
|
|
{
|
|
Action action = listeners[i] as Action;
|
|
action?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Dispatch<T>(System.Enum type, T arg0)
|
|
{
|
|
//Debug.Log($"globalevent1 type: {type}");
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
for (var i = listeners.Count - 1; i >= 0; i--) {
|
|
Action<T> action = listeners[i] as Action<T>;
|
|
action?.Invoke(arg0);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Dispatch<T, U>(System.Enum type, T arg0, U arg1)
|
|
{
|
|
//Debug.Log($"globalevent2 type: {type}");
|
|
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
for (var i = listeners.Count - 1; i >= 0; i--)
|
|
{
|
|
Action<T, U> action = listeners[i] as Action<T, U>;
|
|
action?.Invoke(arg0, arg1);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Dispatch<T, U, V>(System.Enum type, T arg0, U arg1, V arg2)
|
|
{
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
for (var i = listeners.Count - 1; i >= 0; i--)
|
|
{
|
|
Action<T, U, V> action = listeners[i] as Action<T, U, V>;
|
|
action?.Invoke(arg0, arg1, arg2);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Dispatch<T, U, V, Z>(System.Enum type, T arg0, U arg1, V arg2, Z arg3)
|
|
{
|
|
List<Delegate> listeners;
|
|
if (events.TryGetValue(type, out listeners))
|
|
{
|
|
for (var i = listeners.Count - 1; i >= 0; i--)
|
|
{
|
|
Action<T, U, V, Z> action = listeners[i] as Action<T, U, V, Z>;
|
|
action?.Invoke(arg0, arg1, arg2, arg3);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|