unity-针对于消息机制的学习 一

消息机制,主要是三个类
** 消 息 类 ** :MMMessage:有两个以上的成员变量或者属性:发送的消息名称,发送的消息内容主体
需要重写构造函数来给成员变量赋值

消息控制中心MMMessageCenter:消息控制类,需要有注册监听事件的方法、注销消息监听事件的方法、发送消息方法,
同时为了方便管理项目只用一个消息机制,消息控制中心写成单例模式,并用字典容器存储所有的消息列表。
消息名称列表MMMessageName:便于项目使用存放大部分的消息名称,随用随取

具体代码如下:

MMMessage.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 消息类
/// </summary>
public class MMMessage  {
    //成员变量   发送消息的名字
    public string Name{
        get;
        private set;
    }
    //成员变量   发送消息的消息主体
    public object Boby {
        get;
        private set;
    }
    //构造函数   传值赋值
    public MMMessage(string name,object boby){
        Name = name;
        Boby = boby;
    }
}

MMMessageCenter.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 消息中心    消息控制类
/// </summary>
public class MMMessageCenter {
    //定义一个消息类委托 类型 
    public delegate void messageDelHandle(MMMessage message);

    //定义消息控制类的单例
    private static MMMessageCenter instance;
    public static MMMessageCenter Instance {
        get {
            //若是instance为空,初始实例化一个消息控制中心
            if (instance == null) {
                instance = new MMMessageCenter();
            }
            return instance;

        }
    }
    //**定义一个字典  <消息名称, 委托消息>    消息列表**
    private Dictionary<string, messageDelHandle> messageMap = new Dictionary<string, messageDelHandle>();

    /// <summary>
    /// **注册监听**
    /// </summary>
    /// <param name="messageName">消息名称</param>
    /// <param name="handle">消息内容</param>
    public void RigisterListener(string messageName, messageDelHandle handle) {
        if (handle == null) return;  //若是消息为空  退出方法
        if (!messageMap.ContainsKey( messageName )) {
            messageMap.Add( messageName, handle );
        }//若是消息名称不存在,添加到消息列表
    }

    /// <summary>
    /// **移除/注销监听**
    /// </summary>
    /// <param name="messageName">消息名称</param>
    /// <param name="handle">消息内容</param>
    public void RemoveoListener(string messageName, messageDelHandle handle) {
        if (!messageMap.ContainsKey( messageName ))
            return; //若是消息列表里没有这个消息名称键值,已经注销直接退出
        messageMap[ messageName ] -= handle;  //若是存在,去除当前handle的消息内容
        if (messageMap[ messageName ] == null) {
            messageMap.Remove( messageName );
        }   //若是消息列表里当前消息数量为空,则清除该消息名称

    }
    /// <summary>
    /// **发送消息**
    /// </summary>
    /// <param name="messageName">消息名字</param>
    /// <param name="boby">消息主体,可以为空</param>
    public void sendMessage(string messageName,object boby = null) {
        if (!messageMap.ContainsKey( messageName ))
            return;  //若是消息名称不存在   返回
        messageDelHandle handle;  //声明定义
        messageMap.TryGetValue(messageName,out handle);
        if (handle != null) {
            handle(new MMMessage(messageName,boby));
        }
    }
}

MMMessageName.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 发送的消息名
/// </summary>
public class MMMessageName {

    public const string START_UP = "startUp";
}

使用
发送消息代码:在需要发送的时候

void Start () {
        //发送消息
        MMMessageCenter.Instance.sendMessage(MMMessageName.START_UP);
}

接受消息代码:在需要接受和销毁的时候

	void Awake () {
        //注册监听事件
        MMMessageCenter.Instance.RigisterListener( MMMessageName.START_UP, StartUp );
	}

    private void StartUp(MMMessage message) {
        Debug.Log("游戏启动");
    }
    private void OnDestroy() {
        //注销移除监听事件
        MMMessageCenter.Instance.RemoveoListener( MMMessageName.START_UP, StartUp );
    }

猜你喜欢

转载自blog.csdn.net/Mihongzhong/article/details/90212873
今日推荐