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

}

 

转载于:https://www.cnblogs.com/wzqoydn/p/7690717.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值