zoukankan      html  css  js  c++  java
  • c#事件管理器

    c#事件管理器

    namespace FunctionCallBack
    
    {
    
    //事件类型的定义
    
        public enum EventType
    
        {
    
            kaishi,
    
            jinxing,
    
            jieshu,
    
     
    
        }
    
    //事件的数据结构
    
        public class BEvent
    
        {
    
            public EventType type;
    
            public string massage;
    
    }
    
    //委托
    
        public  delegate void SendMassage();
    
    //事件管理器
    
        public class ListenerManager
    
        {
    
            private static ListenerManager instance;
    
            public static ListenerManager Instance
    
            {
    
                get
    
                {
    
                    if (instance == null)
    
                    {
    
                        instance = new ListenerManager();
    
                    }
    
                    return instance;
    
                }
    
                private set
    
                {
    
                    instance = value;
    
                }
    
            }
    
    //事件的容器
    
            public  Dictionary<BEvent, SendMassage> dicListener = new Dictionary<BEvent, SendMassage>();
    
     
    
    //添加要分发的事件
    
            public bool AddEventListener(BEvent bEvent, SendMassage funcCallBack)
    
            {
    
                if (!dicListener.ContainsKey(bEvent))
    
                {
    
                    dicListener.Add(bEvent, funcCallBack);
    
                    return true;
    
                }
    
                else
    
                {
    
                    return false;
    
                }
    
                
    
            }
    
    //移除不分发的事件
    
            public void DeleteEventListener(BEvent bEvent)
    
            {
    
                if (dicListener.ContainsKey(bEvent))
    
                {
    
                    dicListener.Remove(bEvent);
    
                }
    
     
    
            }
    
    //按照事件类型进行分发
    
            public void DisPatch(BEvent bEvent)
    
            {
    
                foreach(BEvent bevent in dicListener.Keys)
    
                {
    
                    if (bevent.type== bEvent.type)
    
                    {
    
                        dicListener[bevent]();
    
                    }
    
                    else
    
                    {
    
                        Debug.Log(bEvent.massage+ " is not exist");
    
                    }
    
                    // Debug.Log(bevent.GetHashCode());
    
     
    
                }
    
            }
    
     
    
        }
    
    }
    
     
    
    //事件使用例子
    
       void Start()
    
        {
    
            BEvent bEvent = new BEvent();
    
            bEvent.massage= "sdf";
    
            bEvent.type = FunctionCallBack.EventType.jieshu;
    
            BEvent bEvent1 = new BEvent();
    
            bEvent1.massage= "df";
    
            bEvent1.type = FunctionCallBack.EventType.jinxing;
    
            ListenerManager.Instance.AddEventListener(bEvent, this.CallBack);
    
            ListenerManager.Instance.AddEventListener(bEvent1, this.CallBack1);
    
           
    
            //man = GetComponent<NavMeshAgent>();
    
        }
    
     
    
        private void CallBack1()
    
        {
    
            Debug.Log("this function1 callback sucess");
    
        }
    
     
    
        // Update is called once per frame
    
        void Update()
    
        {
    
            if (Input.GetKeyDown(KeyCode.A))
    
            {
    
                BEvent bEvent = new BEvent();
    
                bEvent.massage= "sdf";
    
                bEvent.type = FunctionCallBack.EventType.jieshu;
    
                ListenerManager.Instance.DisPatch(bEvent);
    
            }
    
         }   
    
        }
    
        public void CallBack()
    
        {
    
            Debug.Log("this function callback success");
    
    }
  • 相关阅读:
    上学要迟到了【最短路转化】
    解方程【狄利克雷卷积+莫比乌斯反演+积性函数】
    FFT
    min25 筛
    Easy【生成函数】
    CF1406D-Three Sequences
    Alice和Bob赌糖果【赌徒破产模型】
    记MySQL自增主键修改无效的问题
    JVM学习笔记(十一、JDK分析工具)
    JVM学习笔记(十、GC3-垃圾回收机制)
  • 原文地址:https://www.cnblogs.com/wzqoydn/p/7690717.html
Copyright © 2011-2022 走看看