Unity 简易监听框架

全局维护一个字典,字典中的key为字符串或者自定义类型,value为委托,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public delegate void CallBack();
public delegate void CallBack<T>(T t);
public delegate void CallBack<T, D>(T t, D d);
public delegate void CallBack<T, D, U>(T t, D d, U u);

public class Messenger
{
    public static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();

    static void OnListenerAdding(string eventkey, Delegate listenerDelegate)
    {
        if (!eventTable.ContainsKey(eventkey))
        {
            eventTable.Add(eventkey, null);
        }
    }
    public static void AddListener(string eventtype, CallBack handler)
    {
        OnListenerAdding(eventtype, handler);
        eventTable[eventtype] = (eventTable[eventtype] as CallBack) + (handler);
    }

    public static void AddListener<T>(string eventtype, CallBack<T> handler)
    {
        OnListenerAdding(eventtype, handler);
        eventTable[eventtype] = (eventTable[eventtype] as CallBack<T>) + (handler);
    }
    public static void AddListener<T, D>(string eventtype, CallBack<T, D> handler)
    {
        OnListenerAdding(eventtype, handler);
        eventTable[eventtype] = (eventTable[eventtype] as CallBack<T, D>) + (handler);
    }
    public static void AddListener<T, D, U>(string eventtype, CallBack<T, D, U> handler)
    {
        OnListenerAdding(eventtype, handler);
        eventTable[eventtype] = (eventTable[eventtype] as CallBack<T, D, U>) + (handler);
    }
    static void OnBroadcast(string eventtype)
    {
        if (!eventTable.ContainsKey(eventtype))
        {
            Debug.LogError("不包含此监听");
            return;
        }
    }

    public static void Broadcast(string eventtype)
    {
        OnBroadcast(eventtype);
        CallBack callback;
        if (eventTable.ContainsKey(eventtype))
        {
            callback = eventTable[eventtype] as CallBack;
            callback?.Invoke();//如果不为空调用,unity2017以下不可简写
        }
    }
    public static void Broadcast<T>(string eventtype,T t)
    {
        OnBroadcast(eventtype);
        CallBack<T> callback;
        if (eventTable.ContainsKey(eventtype))
        {
            callback = eventTable[eventtype] as CallBack<T>;
            callback?.Invoke(t);//如果不为空调用,unity2017以下不可简写
        }
    }
    public static void Broadcast<T,D>(string eventtype, T t,D d)
    {
        OnBroadcast(eventtype);
        CallBack<T,D> callback;
        if (eventTable.ContainsKey(eventtype))
        {
            callback = eventTable[eventtype] as CallBack<T,D>;
            callback?.Invoke(t, d);//如果不为空调用,unity2017以下不可简写
        }
    }
    public static void Broadcast<T, D,U>(string eventtype, T t, D d,U u)
    {
        CallBack<T, D,U> callback;
        if (eventTable.ContainsKey(eventtype))
        {
            callback = eventTable[eventtype] as CallBack<T, D,U>;
            callback?.Invoke(t, d, u); //如果不为空调用,unity2017以下不可简写
        }
    }
}

  

posted @ 2017-11-30 16:54  飞天艾特小猪  阅读(229)  评论(0编辑  收藏  举报