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以下不可简写
            }
        }
    }
    

      

  • 相关阅读:
    Entity Framework 教程
    C# yield
    表达式树系列文章汇总
    C#中静态与非静态方法比较
    谈谈对Spring IOC的理解
    工厂方法模式与IoC/DI
    通过配置的方式Autofac 《第三篇》
    Autofac 组件、服务、自动装配 《第二篇》
    Autofac 解释第一个例子 《第一篇》
    SQL Server索引调优系列
  • 原文地址:https://www.cnblogs.com/lzy575566/p/7929861.html
Copyright © 2011-2022 走看看