unity 事件监听系统

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

/****************************************************
    文件:EventManager.cs
	作者:风生水虎
    邮箱: [email protected]
*****************************************************/

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 eventDic = new Dictionary();

    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(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(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(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(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 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(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(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(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(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 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(EventType type,T t)
    {
        if (instance.eventDic.ContainsKey(type))
        {
            if (instance.eventDic[type] != null)
            {
                if (instance.eventDic[type] is Action)
                {
                    ((Action)instance.eventDic[type]).Invoke(t);
                }
            }
            else
            {
                instance.eventDic.Remove(type);
            }
        }
    }
    public static void Fire(EventType type, T t,T1 t1)
    {
        if (instance.eventDic.ContainsKey(type))
        {
            if (instance.eventDic[type] != null)
            {
                if (instance.eventDic[type] is Action)
                {
                    ((Action)instance.eventDic[type]).Invoke(t, t1);
                }
            }
            else
            {
                instance.eventDic.Remove(type);
            }
        }
    }
    public static void Fire(EventType type, T t, T1 t1,T2 t2)
    {
        if (instance.eventDic.ContainsKey(type))
        {
            if (instance.eventDic[type] != null)
            {
                if (instance.eventDic[type] is Action)
                {
                    ((Action)instance.eventDic[type]).Invoke(t, t1, t2);
                }
            }
            else
            {
                instance.eventDic.Remove(type);
            }
        }
    }
    public static void Fire(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)
                {
                    ((Action)instance.eventDic[type]).Invoke(t, t1, t2, t3);
                }
            }
            else
            {
                instance.eventDic.Remove(type);
            }
        }
    }
}

public enum EventType
{
    AcceptTask = 1,
    CheckTask = 2 ,
    FinishTask = 3,
}

自己写项目想出来的 如果有问题 可以QQ找我 说明来由

你可能感兴趣的:(unity工具)