unity进阶学习笔记:消息框架

1 使用消息框架的目的

对于小型游戏,可能不需要任何框架,而是让各个游戏脚本直接相互通信。如要实现玩家受到攻击血量减少,通过玩家控制类向血条脚本发送消息减少血量。但是这样直接通信会导致各脚本通信关系记为复杂,并且每一个脚本都和多个脚本有联系,导致维护和更新十分困难

我们利用上节课讲的管理类,可以将一类脚本由一个管理类控制,如将玩家的功能放在玩家管理类下,将血条,背包等UI组件放在UI管理类下。这样要减少玩家血量,可以让玩家控制类向UI管理类发消息,然后UI管理类来改变血条。该方案对大部分小型游戏适用,但对于大型游戏或者网游,一旦管理类过多依然难以管理

我们在管理类基础上再封装一层消息框架用于管理类间的通信。这样每一个类会和消息框架通信,由消息框架找到对于管理类,再由管理类控制对于脚本。
在这里插入图片描述

编写消息框架

1 消息类:用于保存要传输的消息类型及内容

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

public class Message
{
    
    
    public byte Type;
    public int Command;
    public object Content;

    public Message(byte type, int command, object content) {
    
    
        Type = type;
        Command = command;
        Content = content;
    }

}

这里我们的消息类定义三个参数Type, Command, Content。其中Type代表消息类型,这类大概很少,因此使用byte型保存即可以减少传输数据量。Command为具体的消息命令,使用int保存,之后可以定义不同值所对应的命令内容。Content为要传入的消息参数,类型不确定故设为object

为了明确各个类型和指令的具体含义,我们一般设置一个全局参数库用于存储各个指令名称和对应值,如下:

public class MessageType {
    
    
    // type
    public static byte Type_Audio = 1;
    public static byte Type_UI = 2;
    public static byte Type_Player = 3;
    // sound command
    public static int Audio_Play = 100;
    public static int Audio_Stop = 101;
    public static int Audio_Pause = 102;
    // UI command
    public static int UI_ShowPanal = 201;
    public static int UI_AddScore = 202;
    public static int UI_ShowShop = 203;
}

2 管理组件:

这里我们创建一个类MonoBase,里面多一个虚拟方法ReceiveMessage用于接受消息。之后的组件程序全部继承MonoBase类实现消息接受功能

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

public class MonoBase : MonoBehaviour
{
    
    

    // override in child class
    public virtual void ReceiveMessage(Message message) {
    
    

    }

    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }
}

3 管理器:

管理器用于管理各个组件。这里我们创建的管理基类要实现以下方法:

1 存储其下所有的组件
2 让新组件可以注册进管理类
3 接收由消息管理器发布的消息,如果消息类型匹配将消息向下传递到各组件

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

public abstract class ManagerBase : SingletonBase<ManagerBase>
{
    
    

    // store all components
    public List<MonoBase> Monos = new List<MonoBase>();

    // register a component onto the manager
    public void Register(MonoBase mono) {
    
    
        if (!Monos.Contains(mono)) {
    
    
            Monos.Add(mono);
        }
    }


    public virtual void ReceiveMessage(Message message) {
    
    
        // discard unmatched message type
        if (message.Type != GetMessageType()) {
    
    
            return;
        }
        foreach (var mono in Monos) {
    
    
            mono.ReceiveMessage(message);
        }
    }

    public abstract byte GetMessageType();

}

4 消息中心:

消息中心用于管理各个管理类,因此消息中心也具有保存管理类列表和注册新管理类的作用。消息类作为消息系统发送端,用于向各个管理类发送信息,管理类会自动屏蔽非本类型的消息(实现在上面管理类代码)

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

public class MessageCenter : MonoBehaviour
{
    
    

    // store all managers
    public List<ManagerBase> Managers = new List<ManagerBase>();

    // register a new manager
    public void Register(ManagerBase manager) {
    
    
        if (!Managers.Contains(manager)) {
    
    
            Managers.Add(manager);
        }
    }

    // send message
    public void SendCustomMessage(Message message) {
    
    
        foreach (var manager in Managers) {
    
    
            manager.ReceiveMessage(message);
        }
    }
}

至此整个消息框架搭建完毕

在下面我们使用一个简单的吃金币游戏来使用消息框架

1 搭建游戏场景
在这里插入图片描述
创建一个平面,加上灰色材质

创建一个胶囊体作为角色,添加上红色材质,RigidBody组件(设置为Kinetic并冻结旋转),将tag设为Player

创建圆柱体作为金币,添加黄色材质,将碰撞器设为Trigger,新建tag Coin并将金币物体加上Coin tag。复制多份金币

创建UI Canvas,并在左上角加上Panel,Panal锚点放在屏幕左上角。在Panel上加上(旧版)Text用于显示分数

创建空物体用于挂载MessageCenter

2 UIManager脚本

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

public class UIManager : ManagerBase
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        // register to message center
        MessageCenter.Instance.Register(this);
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }

    public override byte GetMessageType() {
    
    
        return MessageType.Type_UI;
    }
}

UIManager继承ManagerBase。在初始化UIManager时将其注册到MessageCenter里。这么我们重写ManagerBase里的GetMessageType,让其返回MessageType.Type_UI。ReceiveMessage方法不需要重写

3 Panal脚本

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

public class Panal : MonoBase
{
    
    

    public Text text;

    // Start is called before the first frame update
    void Start()
    {
    
    
        UIManager.Instance.Register(this);
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }

    public override void ReceiveMessage(Message message) {
    
    
        base.ReceiveMessage(message);
        if (message.Command == MessageType.UI_AddScore) {
    
    
            int score = (int)message.Content;
            // display score
            text.text = "score: " + score;
        }
    }
}

Panal用于控制积分表。在初始化时在UIManager中注册。在ReceiveMessage方法里调用基类的ReceiveMessage得到MessageCenter里发布的消息

3 PlayerManager脚本

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

public class PlayerManager : MonoBehaviour
{
    
    

    int score = 0;

    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector3 dir = new Vector3(horizontal, 0, vertical);
        if (dir != Vector3.zero) {
    
    
            transform.Translate(dir * 5 * Time.deltaTime);
        }
    }

    private void OnTriggerEnter(Collider other) {
    
    
        if (other.tag == "Coin") {
    
    
            score += 1;
            Destroy(other.gameObject);
            // send message
            MessageCenter.Instance.SendCustomMessage(new Message(MessageType.Type_UI, MessageType.UI_AddScore, score));
        }
    }

}

角色控制部分都是unity很基础内容,不额外说明。在该类里调用MessageCenter发布了玩家分数score

总结整个通信流程:
1 PlayerManager类调用MessageCenter发布Type_UI类型,UI_AddScore指令,数据为score

2 MessageCenter调用各个管理器的ReceiveMessage方法,其中Type_UI类型信息被UIManager类获取返回给子类Panal的ReceiveMessage

3 ReceiveMessage得到信息,在UI上显示出分数

消息框架更新:(2023/5/12 添加):
在旧版消息框架中,当我们继承ManagerBase构造具体管理器类时会出现以下问题:ManagerBase对应的单例对象ManagerBase.Instance的数据类型始终为ManagerBase,而非其子类,并且该类型无法通过向下转换变为子类。

无法向下转换原因:

基类 a = new 基类
基类 b = new 子类

在以上情况下,只有b可以被向下转换,因为b实际上保存了子类所有内容,只是编译时会被识别为基类。而a由于对象本身就为基类,不具有子类里面更多的内容,因此无法转换为内容更多的子类,强转结果为null(这里可以类比 1.1可以强转为1,但1不能强转为1.1)

我暂时的解决方法是在ManagerBase中放弃继承SingletonBase,而是直接使用泛型并自己重写一套单例构造方法,如以下代码

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

public abstract class ManagerBase<T> : MonoBehaviour where T : MonoBehaviour
{
    
    

    private static T instance;
    public static T Instance {
    
    
        get {
    
    
            return instance;
        }
    }

    protected void Awake()
    {
    
    
       instance = this as T; 
    }

    
    protected void OnDestroy()
    {
    
    
        instance = null;
    }

    // store all components
    public List<MonoBase> Monos = new List<MonoBase>();

    // register a component onto the manager
    public void Register(MonoBase mono) {
    
    
        if (!Monos.Contains(mono)) {
    
    
            Monos.Add(mono);
        }
    }


    public virtual void ReceiveMessage(Message message) {
    
    
        // discard unmatched message type
        if (message.Type != GetMessageType()) {
    
    
            return;
        }
        foreach (var mono in Monos) {
    
    
            mono.ReceiveMessage(message);
        }
    }

    public abstract byte GetMessageType();

}

同时对MessageCenter进行修改。这里由于ManagerBase里的泛型必须填充为MonoBehavior子类,我们可以将MonoBehavior作为MessageCenter中填充的泛型类

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

public class MessageCenter : SingletonBase<MessageCenter> {
    
    

    // store all managers
    public List<ManagerBase<MonoBehaviour>> Managers = new List<ManagerBase<MonoBehaviour>>();

    // register a new manager
    public void Register(ManagerBase<MonoBehaviour> manager) {
    
    
        if (!Managers.Contains(manager)) {
    
    
            Managers.Add(manager);
        }
    }

    // send message
    public void SendCustomMessage(Message message) {
    
    
        foreach (var manager in Managers) {
    
    
            manager.ReceiveMessage(message);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Raine_Yang/article/details/130499972