一个简单的事件分发机制

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UniEventDispatcher
{
    /// 
    /// 定义事件分发委托
    /// 
    public delegate void OnNotification(Notification notific);

    /// 
    ///通知中心 
    /// 
    public class NotificationCenter
    {

        /// 
        /// 通知中心单例
        /// 
        //private static NotificationCenter instance = null;
        //public static NotificationCenter Instance
        //{
        //    get
        //    {
        //        if (instance == null)
        //        {
        //            instance = new NotificationCenter();
        //            return instance;
        //        }
        //        return instance;
        //    }
        //}


        /// 
        /// 存储事件的字典
        /// 
        private static Dictionary eventListeners = new Dictionary();

        /// 
        /// 注册事件
        /// 
        /// 事件Key
        /// 事件监听器
        public static void AddEventListener(string eventKey, OnNotification eventListener)
        {
            if (!eventListeners.ContainsKey(eventKey))
            {
                eventListeners.Add(eventKey, eventListener);
            }
        }

        /// 
        /// 移除事件
        /// 
        /// 事件Key
        public static void RemoveEventListener(string eventKey)
        {
            if (!eventListeners.ContainsKey(eventKey))
                return;

            eventListeners[eventKey] = null;
            eventListeners.Remove(eventKey);
        }

        /// 
        /// 分发事件
        /// 
        /// 事件Key
        /// 通知
        public static void DispatchEvent(string eventKey, Notification notific)
        {
            if (!eventListeners.ContainsKey(eventKey))
                return;
            eventListeners[eventKey](notific);
        }

        /// 
        /// 分发事件
        /// 
        /// 事件Key
        /// 发送者
        /// 通知内容
        public static void DispatchEvent(string eventKey, GameObject sender, object param)
        {
            if (!eventListeners.ContainsKey(eventKey))
                return;
            eventListeners[eventKey](new Notification(sender, param));
        }

        /// 
        /// 分发事件
        /// 
        /// 事件Key
        /// 通知内容
        public static void DispatchEvent(string eventKey, object param)
        {
            if (!eventListeners.ContainsKey(eventKey))
                return;
            eventListeners[eventKey](new Notification(param));
        }

        /// 
        /// 是否存在指定事件的监听器
        /// 
        /// true if this instance has event listener the specified eventKey; otherwise, false.
        /// Event key.
        public static Boolean HasEventListener(string eventKey)
        {
            return eventListeners.ContainsKey(eventKey);
        }
    }
}


using System;
using UnityEngine;

namespace UniEventDispatcher
{
    public class Notification
    {
        /// 
        /// 通知发送者
        /// 
        public GameObject sender;

        /// 
        /// 通知内容
        /// 备注:在发送消息时需要装箱、解析消息时需要拆箱
        /// 所以这是一个糟糕的设计,需要注意。
        /// 
        public object param;

        /// 
        /// 构造函数
        /// 
        /// 通知发送者
        /// 通知内容
        public Notification(GameObject sender, object param)
        {
            this.sender = sender;
            this.param = param;
        }

        /// 
        /// 构造函数
        /// 
        /// 
        public Notification(object param)
        {
            this.sender = null;
            this.param = param;
        }

        /// 
        /// 实现ToString方法
        /// 
        /// 
        public override string ToString()
        {
            return string.Format("sender={0},param={1}", this.sender, this.param);
        }
    }
}

你可能感兴趣的:(一个简单的事件分发机制)