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");
    
    }
  • 相关阅读:
    yml 配置文件注入
    STS 自动生成 getter 和 setter
    maven build 失败
    navicat 使用
    STS 设置 注解提示
    windows下安装Mysql
    安装 mysql
    用Navicat Premium 操作MySQL数据库
    渐变显示渐变消失的BackgroundView
    基于dispatch_after封装YXTimer
  • 原文地址:https://www.cnblogs.com/wzqoydn/p/7690717.html
Copyright © 2011-2022 走看看