一个简单的事件分发机制

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

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

    /// <summary>
    ///通知中心 
    /// </summary>
    public class NotificationCenter
    {

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


        /// <summary>
        /// 存储事件的字典
        /// </summary>
        private static Dictionary<string, OnNotification> eventListeners = new Dictionary<string, OnNotification>();

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

        /// <summary>
        /// 移除事件
        /// </summary>
        /// <param name="eventKey">事件Key</param>
        public static void RemoveEventListener(string eventKey)
        {
            if (!eventListeners.ContainsKey(eventKey))
                return;

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

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

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

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

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


using System;
using UnityEngine;

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

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

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

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="param"></param>
        public Notification(object param)
        {
            this.sender = null;
            this.param = param;
        }

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容