简易消息管理器EventCenter

/*
 * 日期:2017年7月4日
 * 功能说明:消息中心,添加侦听,移除侦听,消息分发
 * 
 */
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BOFramework
{
    /// 
    /// 消息类,所有侦听消息和发送消息的参数,均以此为类型
    /// 缺点:只能以此为参数类型,需要从object类型转到响应类型
    /// 
    public class EventMessage
    {
        public GameObject Sender;
        public object Param;

        public EventMessage(GameObject _sender, object _param)
        {

            this.Sender = _sender;
            this.Param = _param;
        }
        public EventMessage(object _param)
        {

            this.Sender = null;
            this.Param = _param;
        }
        public override string ToString()
        {
            return string.Format("sender={0},param={1}", this.Sender, this.Param);
        }
    }
    public delegate void EventDelegate(EventMessage msg);

    public class EventCenter
    {

        private static Dictionary
   
   
    
     DicEventData = new Dictionary
    
    
     
     ();
        private static Dictionary
     
     
      
       DicEventDatas = new Dictionary
      
      
       
       ();

        public static void OnListenerAdding(string _msgKey,Delegate _del)
        {
            if (!DicEventDatas.ContainsKey(_msgKey))
            {
                DicEventDatas.Add(_msgKey,null);
            }

            DicEventDatas[_msgKey] = _del;
        }
        

        /// 
       
        /// 添加监听事件 /// 
        /// 
       
       
        /// 
       
       
        public static void AddListener(string _msgKey, EventDelegate _event)
        {
            if(!DicEventData.ContainsKey(_msgKey))
            {
                DicEventData.Add(_msgKey, null);
            }

            DicEventData[_msgKey] += _event;
        }


        /// 
       
        /// 移除指定监听 /// 
        /// 
       
       
        public static void RemoveListener(string _msgKey)
        {
            if (!DicEventData.ContainsKey(_msgKey))
            {
                DicEventData[_msgKey] = null;
                DicEventData.Remove(_msgKey);
            }
        }
        /// 
       
        /// 移除所有监听 /// 
        public static void RemoveAllListener()
        {
            DicEventData.Clear();
        }

        public static void BroadCastMessage(string _msgKey)
        {
            BroadCastMessage(_msgKey,null);
        }
        /// 
       
        /// 广播消息,带参数 /// 
        /// 
       
       
        /// 
       
       
        public static void BroadCastMessage(string _msgKey, EventMessage _msg)
        {
            
            EventDelegate _eDeleg;
            if (DicEventData.TryGetValue(_msgKey, out _eDeleg))
            {
                _eDeleg(_msg);
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值