unity 事件监听系统

自己写的事件监听系统
用泛型避免了拆箱装箱,
目前 : 一个事件对应一种委托类型
如需一个事件对应多种委托类型 需 自行扩展 并不难
代码如下:

/****************************************************
    文件: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找我 说明来由

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值