• 事件中心(可用于不同模块间的通信,用于解耦)


    单例模式

    using UnityEngine;
    using System.Collections;

    namespace SingleTonBase
    {
    public class SingleTonBase<T> where T : new()
    {

    private static T _instance;
    public static T GetInstance()
    {
    if (_instance == null)
    _instance = new T();
    return _instance;
    }


    }
    public class SingleTonBehaviorBase<T> : MonoBehaviour where T : MonoBehaviour
    {
    private static T _instance;
    public static T GetInstance()
    {
    _instance = GameObject.FindObjectOfType<T>();
    if (_instance == null)
    {
    GameObject goObj = new GameObject(typeof(T).ToString());
    _instance = goObj.AddComponent<T>();
    }
    return _instance;
    }
    }
    }

    消息中心 用于注册消息

    using UnityEngine;
    using System.Collections;

    namespace Message
    {
    public class MessageConst
    {
    public static readonly string MESSAGE_TEST = "TEST";

    }
    }

    事件中心,用于注册分发事件,传递消息

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

    public class NotificationCentre : SingleTonBehaviorBase<NotificationCentre>
    {

    public delegate void MessageDelegate(params object[] obj);
    private Dictionary<string, MessageDelegate> _messageCache = new Dictionary<string, MessageDelegate>();

    /// <summary>
    /// 订阅消息
    /// </summary>
    /// <param name="messageKey">消息的关键字</param>
    /// <param name="md">消息的委托函数</param>
    public void RegisterMessage(string messageKey,MessageDelegate md)
    {
    if (_messageCache.ContainsKey(messageKey))
    {
    Debug.LogError("Don`t register again!!!");
    return;
    }
    _messageCache.Add(messageKey,md);
    }
    /// <summary>
    /// 发送消息
    /// </summary>
    /// <param name="messageKey">消息关键字</param>
    public void SendMessage(string messageKey)
    {
    SendMessage(messageKey,null);
    }
    /// <summary>
    /// 发送消息
    /// </summary>
    /// <param name="messageKey">消息关键字</param>
    /// <param name="messageContent">消息内容</param>
    public void SendMessage(string messageKey, params object[] messageContent)
    {
    if (!_messageCache.ContainsKey(messageKey))
    {
    Debug.LogError("The " + messageKey + "not be registered");
    return;
    }
    _messageCache[messageKey](messageContent);
    }

    public void RemoveMessage(string messageKey)
    {
    if (_messageCache.ContainsKey(messageKey))
    _messageCache.Remove(messageKey);
    }

    //public void RemoveMessage(MessageDelegate md)
    //{
    // if (_messageCache.ContainsValue(md))
    // // _messageCache.Remove();
    //}
    }

    测试脚本

    using UnityEngine;
    using System.Collections;
    using Message;
    public class TestNotification : MonoBehaviour {

    void Awake()
    {
    //订阅
    NotificationCentre.GetInstance().RegisterMessage(MessageConst.MESSAGE_TEST, AcceptMessage);
    }

    void AcceptMessage(params object[] message)
    {
    Debug.Log(message[0]);
    Debug.Log(message[1]);
    }

    void Start () {
    NotificationCentre.GetInstance().SendMessage(MessageConst.MESSAGE_TEST,"Hello Message",this.gameObject);
    }
    void OnDestroy()
    {
    NotificationCentre.GetInstance().RemoveMessage(MessageConst.MESSAGE_TEST);
    }

    }

  • 相关阅读:
    git的学习笔记
    软件工程概论第一周综合测验
    人月神话读后感
    软件工程概论通读第十一章
    软件工程概论通读第十章
    软件工程概论通读第九章
    软件工程概论通读第八章
    软件工程概论通读第七章
    软件工程概论通读第六章
    软件工程概论通读第五章
  • 原文地址:https://www.cnblogs.com/FingerCaster/p/7591305.html
Copyright © 2020-2023  润新知