自己写的事件监听系统
用泛型避免了拆箱装箱,
目前 : 一个事件对应一种委托类型
如需一个事件对应多种委托类型 需 自行扩展 并不难
代码如下:
/****************************************************
文件:EventManager.cs
作者:风生水虎
邮箱: 851267723@qq.com
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class EventManager
{
static EventManager() {
if (instance == null)
instance = new EventManager();
}
private static EventManager instance;
private Dictionary<EventType, object> eventDic = new Dictionary<EventType, object>();
public static void Subscriber(EventType type, Action action)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] is Action)
{
instance.eventDic[type] = (Action)instance.eventDic[type] + action;
}
}
else
{
instance.eventDic.Add(type, action);
}
}
public static void Subscriber<T>(EventType type,Action<T> action)
{
if (instance.eventDic.ContainsKey(type))
{
if(instance.eventDic[type] is Action<T>)
{
instance.eventDic[type] = (Action<T>)instance.eventDic[type] + action;
}
}
else
{
instance.eventDic.Add(type, action);
}
}
public static void Subscriber<T,T1>(EventType type, Action<T,T1> action)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] is Action<T,T1>)
{
instance.eventDic[type] = (Action<T,T1>)instance.eventDic[type] + action;
}
}
else
{
instance.eventDic.Add(type, action);
}
}
public static void Subscriber<T, T1,T2>(EventType type, Action<T, T1,T2> action)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] is Action<T, T1,T2>)
{
instance.eventDic[type] = (Action<T, T1,T2>)instance.eventDic[type] + action;
}
}
else
{
instance.eventDic.Add(type, action);
}
}
public static void Subscriber<T, T1, T2,T3>(EventType type, Action<T, T1, T2,T3> action)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] is Action<T, T1, T2,T3>)
{
instance.eventDic[type] = (Action<T, T1, T2,T3>)instance.eventDic[type] + action;
}
}
else
{
instance.eventDic.Add(type, action);
}
}
public static void UnSubscriber(EventType type, Action action)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] is Action)
{
instance.eventDic[type] = (Action)instance.eventDic[type] - action;
}
}
}
public static void UnSubscriber<T>(EventType type, Action<T> action)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] is Action<T>)
{
instance.eventDic[type] = (Action<T>)instance.eventDic[type] - action;
}
}
}
public static void UnSubscriber<T, T1>(EventType type, Action<T, T1> action)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] is Action<T, T1>)
{
instance.eventDic[type] = (Action<T, T1>)instance.eventDic[type] - action;
}
}
}
public static void UnSubscriber<T, T1, T2>(EventType type, Action<T, T1, T2> action)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] is Action<T, T1, T2>)
{
instance.eventDic[type] = (Action<T, T1, T2>)instance.eventDic[type] - action;
}
}
}
public static void UnSubscriber<T, T1, T2, T3>(EventType type, Action<T, T1, T2, T3> action)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] is Action<T, T1, T2, T3>)
{
instance.eventDic[type] = (Action<T, T1, T2, T3>)instance.eventDic[type] - action;
}
}
}
public static void Fire(EventType type)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] != null )
{
if(instance.eventDic[type] is Action)
{
((Action)instance.eventDic[type]).Invoke();
}
}
else
{
instance.eventDic.Remove(type);
}
}
}
public static void Fire<T>(EventType type,T t)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] != null)
{
if (instance.eventDic[type] is Action<T>)
{
((Action<T>)instance.eventDic[type]).Invoke(t);
}
}
else
{
instance.eventDic.Remove(type);
}
}
}
public static void Fire<T,T1>(EventType type, T t,T1 t1)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] != null)
{
if (instance.eventDic[type] is Action<T, T1>)
{
((Action<T, T1>)instance.eventDic[type]).Invoke(t, t1);
}
}
else
{
instance.eventDic.Remove(type);
}
}
}
public static void Fire<T, T1,T2>(EventType type, T t, T1 t1,T2 t2)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] != null)
{
if (instance.eventDic[type] is Action<T, T1, T2>)
{
((Action<T, T1, T2>)instance.eventDic[type]).Invoke(t, t1, t2);
}
}
else
{
instance.eventDic.Remove(type);
}
}
}
public static void Fire<T, T1, T2,T3>(EventType type, T t, T1 t1, T2 t2,T3 t3)
{
if (instance.eventDic.ContainsKey(type))
{
if (instance.eventDic[type] != null)
{
if (instance.eventDic[type] is Action<T, T1, T2, T3>)
{
((Action<T, T1, T2, T3>)instance.eventDic[type]).Invoke(t, t1, t2, t3);
}
}
else
{
instance.eventDic.Remove(type);
}
}
}
}
public enum EventType
{
AcceptTask = 1,
CheckTask = 2 ,
FinishTask = 3,
}
自己写项目想出来的 如果有问题 可以QQ找我 说明来由