zoukankan      html  css  js  c++  java
  • 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以下不可简写
            }
        }
    }
    

      

  • 相关阅读:
    二、静、动态代理(9~10)~~~~
    luogu P3572 [POI2014]PTA-Little Bird 单调队列优化dp
    luogu P6113 【模板】一般图最大匹配 带花树
    Codeforces Round #646 (Div. 2) C——Game On Leaves 思维
    Codeforces Round #646 (Div. 2) E——Tree Shuffling 思维
    luogu P2979 [USACO10JAN]Cheese Towers S 变形dp背包
    luogu P6577 【模板】二分图最大权完美匹配
    Educational Codeforces Round 88 (Rated for Div. 2) D
    Codeforces Round #645 (Div. 2) E
    Codeforces Round #645 (Div. 2) D
  • 原文地址:https://www.cnblogs.com/lzy575566/p/7929861.html
Copyright © 2011-2022 走看看