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

这篇具有很好参考价值的文章主要介绍了unity进阶学习笔记:消息框架。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1 使用消息框架的目的

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

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

我们在管理类基础上再封装一层消息框架用于管理类间的通信。这样每一个类会和消息框架通信,由消息框架找到对于管理类,再由管理类控制对于脚本。
unity进阶学习笔记:消息框架

编写消息框架

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 搭建游戏场景
unity进阶学习笔记:消息框架
创建一个平面,加上灰色材质

创建一个胶囊体作为角色,添加上红色材质,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中填充的泛型类文章来源地址https://www.toymoban.com/news/detail-461730.html

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);
        }
    }
}

到了这里,关于unity进阶学习笔记:消息框架的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity学习笔记--如何优雅简便地利用对象池生成游戏对象(进阶版)LRU + 对象池

    之前写过一篇关于对象池的文章,现在来看写的并不是很好,所以来考虑优化下。 现在来看一年前写的代码,越看越不能入目hhh Unity学习笔记–如何优雅简便地利用对象池生成游戏对象 Unity学习笔记–使用 C# 开发一个 LRU PoolManager.cs BaseFactory.cs 创建 Factory 创建 object 创建 Bu

    2024年02月13日
    浏览(29)
  • GameFrameWork框架(Unity3D)使用笔记(八) 实现场景加载进度条

            游戏在转换场景的时候,需要花费时间来加载相关的资源。而这个过程往往因为游戏场景的规模和复杂度以及玩家电脑配置的原因花费一小段时间(虽然这个项目里用不到)。         所以,如果这一小段时间,画面就卡在这里,啥也做不了,玩家也不知道啥时候能

    2024年02月02日
    浏览(34)
  • 【Unity学习笔记】Unity TestRunner使用

    转载请注明出处:🔗https://blog.csdn.net/weixin_44013533/article/details/135733479 作者:CSDN@|Ringleader| 参考: Input testing Getting started with Unity Test Framework HowToRunUnityUnitTest 如果对Unity的newInputSystem感兴趣可以参看我这篇文章:【Unity学习笔记】第十二 · New Input System 及其系统结构 和 源码浅

    2024年01月22日
    浏览(26)
  • 【Unity】 HTFramework框架(四十四)【进阶篇】指令系统

    更新日期:2023年5月29日。 Github源码:[点我获取源码] Gitee源码:[点我获取源码] 指令系统 为Unity动态修补程序、热更新等提供了另一种补充方案,我们可以将任意一段 指令代码 即时编译并执行(请放心,即时编译的性能开销极低),达到运行时随意修改程序功能的骚操作。

    2024年02月09日
    浏览(31)
  • GoLong的学习之路,进阶,RabbitMQ (消息队列)

    快有一周没有写博客了。前面几天正在做项目。正好,项目中需要MQ(消息队列),这里我就补充一下我对mq的理解。其实在学习java中的时候,自己也仿照RabbitMQ自己实现了一个单机的mq,但是mq其中一个特点也就是,分布式我在项目中没有涉及。这里我用go语言将RabbitMQ的操作

    2024年02月03日
    浏览(38)
  • 【大数据进阶第三阶段之ClickHouse学习笔记】ClickHouse的简介和使用

    ClickHouse是一种 列式数据库管理系统(DBMS) ,专门用于 高性能数据分析 和 数据仓库 应用。它是一个开源的数据库系统,最初由俄罗斯搜索引擎公司Yandex开发,用于满足 大规模数据分析和报告的需求 。 开源地址:GitHub - ClickHouse/ClickHouse: ClickHouse® is a free analytics DBMS for bi

    2024年02月03日
    浏览(29)
  • 【Unity】 HTFramework框架(四十五)【进阶篇】指令系统-自定义指令

    更新日期:2023年6月19日。 Github源码:[点我获取源码] Gitee源码:[点我获取源码] 指令系统详解:【Unity】 HTFramework框架(四十四)【进阶篇】指令系统。 使用 HTFramework 的 指令系统 ,可以将一段指令代码即时编译并执行。 例如,我需要这样一个功能: 新建一个游戏物体,将

    2024年02月11日
    浏览(68)
  • DotNet VOL.Core框架学习使用笔记(持续更新)

     2023-6-8 下拉框绑定数据源的增加设置在 系统设置-下拉框绑定设置里面,重点是根据需要用sql查出数据源。 2023-6-12 从控制器外层的sys_regionController部分类调试来看 _isys_RegionRepository 注入了总是提示为空。 后来了解到了框架约束好像所有的业务都要写到partial文件夹的部分类里

    2024年02月10日
    浏览(26)
  • Unity学习笔记--使用 C# 开发一个 LRU

    什么是 LRU 在计算机系统中,LRU(Least Recently Used,最近最少使用)是一种缓存置换算法。缓存是计算机系统中的一种能够高速获取数据的介质,而缓存置换算法则是在缓存空间不足时,需要淘汰掉部分缓存数据以腾出空间,使得后来需要访问数据能够有更多的缓存空间可用。

    2024年02月13日
    浏览(28)
  • Zinx框架学习 - 消息封装

    之前我们使用Request来保存服务器的数据,很显然使用[]byte来接收数据,没有长度也没有消息类型,接下来就要针对这个消息进行封装 创建消息类型 定义一个基本的message包,会包含消息ID、数据、数据长度三个成员,并提供基本的setter和getter方法 imssage.go接口 message.go实现类

    2024年02月07日
    浏览(18)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包