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");

}

 

posted @ 2017-10-19 10:44  倾城之叹  阅读(2095)  评论(0编辑  收藏  举报