当前位置: 首页 > news >正文

Unity-Mirror网络框架-从入门到精通之Basic示例

文章目录

    • 前言
    • Basic示例
    • 场景元素
    • 预制体元素
    • 代码逻辑
      • BasicNetManager
      • Player逻辑
        • SyncVars属性
        • Server逻辑
        • Client逻辑
      • PlayerUI逻辑
    • 最后

前言

在现代游戏开发中,网络功能日益成为提升游戏体验的关键组成部分。Mirror是一个用于Unity的开源网络框架,专为多人游戏开发设计。它使得开发者能够轻松实现网络连接、数据同步和游戏状态管理。本文将深入介绍Mirror的基本概念、如何与其他网络框架进行比较,以及如何从零开始创建一个使用Mirror的简单网络项目。
在这里插入图片描述

Basic示例

运行结果如下:
在这里插入图片描述
Basic示例是Mirror的最简单的示例,就展示了一个Player玩家的登录UI状态,
但是它却反应了Mirror框架的所有使用流程,可谓麻雀虽小,五脏俱全。
也从正面反应了,Mirror的简单易用性。

场景元素

我们打开场景,可以看到Basic场景中元素很少,除了一个NetworkManager,就是一个MainPanel的UI页面。
NetworkManager:即Mirror启动所需的网络管理器,有且必须有一个。
MainPanel:是用于存放玩家登录后的玩家UI图标的容器而已。
在这里插入图片描述

预制体元素

Basic场景用到的预制体,只有两个,
Player:代表登录后的玩家实体,如果玩家有移动,战斗等逻辑,就在它上面写
PlayerUI:代表玩家登录后,在UI显示的玩家图标,一个Player对应一个PlayerUI。
在这里插入图片描述

代码逻辑

BasicNetManager

namespace Mirror.Examples.Basic
{[AddComponentMenu("")]public class BasicNetManager : NetworkManager{public override void OnServerAddPlayer(NetworkConnectionToClient conn){base.OnServerAddPlayer(conn);Player.ResetPlayerNumbers();}public override void OnServerDisconnect(NetworkConnectionToClient conn){base.OnServerDisconnect(conn);Player.ResetPlayerNumbers();}}
}

BasicNetManager,继承了NetworkManager,因为NetworkManager只是包括了网络管理器的一些基本功能,比如:保持连接,维持NetworkIdentity的同步等基本网络同步功能。
Mirror给我们提供的很多Sample示例,其实都重写了NetworkManager,给我们展示了实现不同类型的项目,如何扩展NetworkManager。
NetworkManager内部有很多的生命周期函数,我们可以重写,并实现自己的目的。
比如这里,重写了OnServerAddPlayer和OnServerDisconnect。分别代表,客户端玩家连接上服务器和断开连接服务器。然后调用Player的函数,更新玩家Number显示。

Player逻辑

Player继承自NetworkBehaviour,是一个网络单元的行为组件。
我们都知道Unity是组件式编程,任何游戏对象如果想实现游戏逻辑,都可以由N个组件组成。
同理,在Mirror中,任何物体想要被同步,必须添加NetworkIdentity组件,代表是一个网络单元,然后只要这个网路单元,想实现其他的同步逻辑,比如属性,RPC调用等行为,就必须添加NetworkBehaviour组件才能实现, 而Player就是一个NetworkBehaviour,实现了Player应有的属性同步以及UI图标刷新功能。

SyncVars属性

Mirror提供了一种非常便利的属性同步机制,即[SyncVar],只要一个属性添加了SyncVar属性,就可以在不同的客户端之间实现同步。
如果添加了Hook参数,还可以实现参数变化时的函数回调。如下代码所示:
playerNumber
playerColor
playerData
这三个参数是可以在网络中进行同步的,且修改后,每个玩家的UI图标的数值都会发生自动更新。

#region SyncVars[Header("SyncVars")]/// <summary>/// This is appended to the player name text, e.g. "Player 01"/// </summary>[SyncVar(hook = nameof(PlayerNumberChanged))]public byte playerNumber = 0;/// <summary>/// Random color for the playerData text, assigned in OnStartServer/// </summary>[SyncVar(hook = nameof(PlayerColorChanged))]public Color32 playerColor = Color.white;/// <summary>/// This is updated by UpdateData which is called from OnStartServer via InvokeRepeating/// </summary>[SyncVar(hook = nameof(PlayerDataChanged))]public ushort playerData = 0;// This is called by the hook of playerNumber SyncVar abovevoid PlayerNumberChanged(byte _, byte newPlayerNumber){OnPlayerNumberChanged?.Invoke(newPlayerNumber);}// This is called by the hook of playerColor SyncVar abovevoid PlayerColorChanged(Color32 _, Color32 newPlayerColor){OnPlayerColorChanged?.Invoke(newPlayerColor);}// This is called by the hook of playerData SyncVar abovevoid PlayerDataChanged(ushort _, ushort newPlayerData){OnPlayerDataChanged?.Invoke(newPlayerData);}#endregion
Server逻辑

Server逻辑什么意思呢?为什么要拆分Server和Client逻辑呢?
这就要在此强调下Mirror的网络架构了,我再开篇的:Unity-Mirror网络框架-从入门到精通之Mirror简介中,专门介绍了Mirror的网络结构和传统的CS网络结构的差异,我们不能像写传统的CS架构下客户端一样
去写Mirror框架中的逻辑。因为Mirror框架中,我们写的代码实际是包含了服务器和客户端,双端逻辑的,这样就省去了一个Server端。只是我们在开发时要注意。有些逻辑需要写在Server中,有些需要写在Client中。这一点是非常重要的。

如下代码所示:
可以理解为这些逻辑属于服务器执行的逻辑。
OnStartServer,就是当一个客户端玩家,在服务器上被激活时,一般用于初始化一些同步网络属性,默认情况下SyncVar属性的值,只能在Server下被修改,然后同步给所有客户端。

[ServerCallback]属性修饰了两个函数ResetPlayerNumber和UpdateData,
代表这两个函数,只能在Server端被调用,客户端是 不能调用的。

        #region Server/// <summary>/// This is invoked for NetworkBehaviour objects when they become active on the server./// <para>This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.</para>/// <para>This will be called for objects on a "host" as well as for object on a dedicated server.</para>/// </summary>public override void OnStartServer(){base.OnStartServer();// Add this to the static Players ListplayersList.Add(this);// set the Player Color SyncVarplayerColor = Random.ColorHSV(0f, 1f, 0.9f, 0.9f, 1f, 1f);// set the initial player dataplayerData = (ushort)Random.Range(100, 1000);// Start generating updatesInvokeRepeating(nameof(UpdateData), 1, 1);}// This is called from BasicNetManager OnServerAddPlayer and OnServerDisconnect// Player numbers are reset whenever a player joins / leaves[ServerCallback]internal static void ResetPlayerNumbers(){byte playerNumber = 0;foreach (Player player in playersList)player.playerNumber = playerNumber++;}// This only runs on the server, called from OnStartServer via InvokeRepeating[ServerCallback]void UpdateData(){playerData = (ushort)Random.Range(100, 1000);}/// <summary>/// Invoked on the server when the object is unspawned/// <para>Useful for saving object data in persistent storage</para>/// </summary>public override void OnStopServer(){CancelInvoke();playersList.Remove(this);}#endregion
Client逻辑

Client逻辑,就是纯粹的客户端显示逻辑。
比如:
OnStartClient.,当玩家在客户端激活时,创建一个指定的UI显示玩家信息
OnStartLocalPlayer:当本机玩家连接成功后,显示画布UI,代表自己登录成功了

 #region Client/// <summary>/// Called on every NetworkBehaviour when it is activated on a client./// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.</para>/// </summary>public override void OnStartClient(){// Instantiate the player UI as child of the Players PanelplayerUIObject = Instantiate(playerUIPrefab, CanvasUI.GetPlayersPanel());playerUI = playerUIObject.GetComponent<PlayerUI>();// wire up all events to handlers in PlayerUIOnPlayerNumberChanged = playerUI.OnPlayerNumberChanged;OnPlayerColorChanged = playerUI.OnPlayerColorChanged;OnPlayerDataChanged = playerUI.OnPlayerDataChanged;// Invoke all event handlers with the initial data from spawn payloadOnPlayerNumberChanged.Invoke(playerNumber);OnPlayerColorChanged.Invoke(playerColor);OnPlayerDataChanged.Invoke(playerData);}/// <summary>/// Called when the local player object has been set up./// <para>This happens after OnStartClient(), as it is triggered by an ownership message from the server. This is an appropriate place to activate components or functionality that should only be active for the local player, such as cameras and input.</para>/// </summary>public override void OnStartLocalPlayer(){// Set isLocalPlayer for this Player in UI for background shadingplayerUI.SetLocalPlayer();// Activate the main panelCanvasUI.SetActive(true);}/// <summary>/// Called when the local player object is being stopped./// <para>This happens before OnStopClient(), as it may be triggered by an ownership message from the server, or because the player object is being destroyed. This is an appropriate place to deactivate components or functionality that should only be active for the local player, such as cameras and input.</para>/// </summary>public override void OnStopLocalPlayer(){// Disable the main panel for local playerCanvasUI.SetActive(false);}/// <summary>/// This is invoked on clients when the server has caused this object to be destroyed./// <para>This can be used as a hook to invoke effects or do client specific cleanup.</para>/// </summary>public override void OnStopClient(){// disconnect event handlersOnPlayerNumberChanged = null;OnPlayerColorChanged = null;OnPlayerDataChanged = null;// Remove this player's UI objectDestroy(playerUIObject);}#endregion

PlayerUI逻辑

PlayerUI比较简单,当Player属性发行变化时,会调用对应的函数,更新PlayerUI显示。

Player中有对应的事件绑定

            // Instantiate the player UI as child of the Players PanelplayerUIObject = Instantiate(playerUIPrefab, CanvasUI.GetPlayersPanel());playerUI = playerUIObject.GetComponent<PlayerUI>();// wire up all events to handlers in PlayerUIOnPlayerNumberChanged = playerUI.OnPlayerNumberChanged;OnPlayerColorChanged = playerUI.OnPlayerColorChanged;OnPlayerDataChanged = playerUI.OnPlayerDataChanged;

PlayerUI中只需要更新Text的显示即可

namespace Mirror.Examples.Basic
{public class PlayerUI : MonoBehaviour{[Header("Player Components")]public Image image;[Header("Child Text Objects")]public Text playerNameText;public Text playerDataText;// Sets a highlight color for the local playerpublic void SetLocalPlayer(){// add a visual background for the local player in the UIimage.color = new Color(1f, 1f, 1f, 0.1f);}// This value can change as clients leave and joinpublic void OnPlayerNumberChanged(byte newPlayerNumber){playerNameText.text = string.Format("Player {0:00}", newPlayerNumber);}// Random color set by Player::OnStartServerpublic void OnPlayerColorChanged(Color32 newPlayerColor){playerNameText.color = newPlayerColor;}// This updates from Player::UpdateData via InvokeRepeating on serverpublic void OnPlayerDataChanged(ushort newPlayerData){// Show the data in the UIplayerDataText.text = string.Format("Data: {0:000}", newPlayerData);}}
}

最后

最后运行成功后,会显示玩家编号和信息,代表玩家连接成功。
在此基础上,我们可以自己发挥想象力扩展一下如:
玩家昵称,在PlayeUI上显示。
用一个模型代表玩家身体,可以再Player中做玩家的位移等。
在这里插入图片描述
好了,这篇文章就到这里,希望对你有所帮助

相关文章:

Unity-Mirror网络框架-从入门到精通之Basic示例

文章目录 前言Basic示例场景元素预制体元素代码逻辑BasicNetManagerPlayer逻辑SyncVars属性Server逻辑Client逻辑 PlayerUI逻辑 最后 前言 在现代游戏开发中&#xff0c;网络功能日益成为提升游戏体验的关键组成部分。Mirror是一个用于Unity的开源网络框架&#xff0c;专为多人…...

CSS 图片廊:网页设计的艺术与技巧

CSS 图片廊&#xff1a;网页设计的艺术与技巧 引言 在网页设计中&#xff0c;图片廊是一个重要的组成部分&#xff0c;它能够以视觉吸引的方式展示图片集合&#xff0c;增强用户的浏览体验。CSS&#xff08;层叠样式表&#xff09;作为网页设计的主要语言之一&#xff0c;提供…...

AI 发展的第一驱动力:人才引领变革

在科技蓬勃发展的当下&#xff0c;AI 成为了时代的焦点&#xff0c;然而其发展并非一帆风顺&#xff0c;究竟什么才是推动 AI 持续前行的关键力量呢&#xff1f; 目录 AI 发展现状剖析 期望与现实的落差 落地困境根源 人才&#xff1a;AI 发展的核心动力​编辑 技术突破的…...

[创业之路-229]:《华为闭环战略管理》-5-平衡记分卡与战略地图

目录 一、平衡记分卡 1. 财务角度&#xff1a; 2. 客户角度&#xff1a; 3. 内部运营角度&#xff1a; 4. 学习与成长角度&#xff1a; 二、BSC战略地图 1、核心内容 2、绘制目的 3、绘制方法 4、注意事项 一、平衡记分卡 平衡记分卡&#xff08;Balanced Scorecard&…...

用uniapp写一个播放视频首页页面代码

效果如下图所示 首页有导航栏&#xff0c;搜索框&#xff0c;和视频列表&#xff0c; 导航栏如下图 搜索框如下图 视频列表如下图 文件目录 视频首页页面代码如下 <template> <view class"video-home"> <!-- 搜索栏 --> <view class…...

【视觉SLAM:八、后端Ⅰ】

视觉SLAM的后端主要解决状态估计问题&#xff0c;它是优化相机轨迹和地图点的过程&#xff0c;从数学上看属于非线性优化问题。后端的目标是结合传感器数据&#xff0c;通过最优估计获取系统的状态&#xff08;包括相机位姿和场景结构&#xff09;&#xff0c;在状态估计过程中…...

PaddleOCROCR关键信息抽取训练过程

步骤1&#xff1a;python版本3.8.20 步骤2&#xff1a;下载代码&#xff0c;安装依赖 git clone https://gitee.com/PaddlePaddle/PaddleOCR.git pip uninstall opencv-python -y # 安装PaddleOCR的依赖 ! pip install -r requirements.txt # 安装关键信息抽取任务的依赖 !…...

用Python操作字节流中的Excel文档

Python能够轻松地从字节流中加载文件&#xff0c;在不依赖于外部存储的情况下直接对其进行读取、修改等复杂操作&#xff0c;并最终将更改后的文档保存回字节串中。这种能力不仅极大地提高了数据处理的灵活性&#xff0c;还确保了数据的安全性和完整性&#xff0c;尤其是在网络…...

python 桶排序(Bucket Sort)

桶排序&#xff08;Bucket Sort&#xff09; 桶排序是一种分布式排序算法&#xff0c;适用于对均匀分布的数据进行排序。它的基本思想是&#xff1a;将数据分到有限数量的桶中&#xff0c;每个桶分别排序&#xff0c;最后将所有桶中的数据合并。 桶排序的步骤&#xff1a; 划…...

Elasticsearch:探索 Elastic 向量数据库的深度应用

Elasticsearch&#xff1a;探索 Elastic 向量数据库的深度应用 一、Elasticsearch 向量数据库简介 1. Elasticsearch 向量数据库的概念 Elasticsearch 本身是一个基于 Lucene 的搜索引擎&#xff0c;提供了全文搜索和分析的功能。随着技术的发展&#xff0c;Elasticsearch 也…...

【每日学点鸿蒙知识】属性变量key、waterflow卡顿问题、包无法上传、Video控件播放视频、Vue类似语法

1、HarmonyOS 属性变量常量是否可以作为object对象的key&#xff1f; a: object new Object() this.a[Constants.TEST_KEY] "456" 可以先定义&#xff0c;再赋值 2、首页点击回到waterflow的首节点&#xff0c;0~index全部节点被重建&#xff0c;导致卡顿 使用s…...

小程序中引入echarts(保姆级教程)

hello hello~ &#xff0c;这里是 code袁~&#x1f496;&#x1f496; &#xff0c;欢迎大家点赞&#x1f973;&#x1f973;关注&#x1f4a5;&#x1f4a5;收藏&#x1f339;&#x1f339;&#x1f339; &#x1f981;作者简介&#xff1a;一名喜欢分享和记录学习的在校大学生…...

基于 Node.js 的 ORM(对象关系映射)工具——Sequelize介绍与使用,并举案例分析

便捷性介绍 支持多种数据库&#xff0c;包括 PostgreSQL、MySQL、MariaDB、SQLite 和 Microsoft SQL Server。Sequelize 提供了丰富的功能&#xff0c;帮助开发者用 JavaScript&#xff08;或 TypeScript&#xff09;代码操作数据库&#xff0c;而无需直接书写 SQL 语句。 Se…...

python 插入排序(Insertion Sort)

插入排序&#xff08;Insertion Sort&#xff09; 插入排序是一种简单的排序算法。它的基本思想是&#xff1a;将数组分为已排序部分和未排序部分&#xff0c;然后逐个将未排序部分的元素插入到已排序部分的正确位置。插入排序类似于整理扑克牌的过程。 插入排序的步骤&#…...

电子应用设计方案81:智能AI冲奶瓶系统设计

智能 AI 冲奶瓶系统设计 一、引言 智能 AI 冲奶瓶系统旨在为父母或照顾者提供便捷、准确和卫生的冲奶服务&#xff0c;特别是在夜间或忙碌时&#xff0c;减轻负担并确保婴儿获得适宜的营养。 二、系统概述 1. 系统目标 - 精确调配奶粉和水的比例&#xff0c;满足不同年龄段婴…...

JAVA高并发总结

JAVA高并发编程总结 在现代应用中&#xff0c;高并发编程是非常重要的一部分&#xff0c;尤其是在分布式系统、微服务架构、实时数据处理等领域。Java 提供了丰富的并发工具和技术&#xff0c;帮助开发者在多线程和高并发的场景下提高应用的性能和稳定性。以下是 Java 高并发编…...

【AIGC】使用Java实现Azure语音服务批量转录功能:完整指南

文章目录 引言技术背景环境准备详细实现1. 基础架构设计2. 实现文件上传功能3. 提交转录任务crul4. 获取转录结果 使用示例结果示例最佳实践与注意事项总结 引言 在当今数字化时代&#xff0c;将音频内容转换为文本的需求越来越普遍。无论是会议记录、视频字幕生成&#xff0c…...

arcgis模版空库怎么用(一)

这里以某个项目的数据为例&#xff1a; 可以看到&#xff0c;属性表中全部只有列标题&#xff0c;无数据内容 可能有些人会认为空库是用来往里面加入信息的&#xff0c;其实不是&#xff0c;正确的用法如下&#xff1a; 一、下图是我演示用的数据&#xff0c;我们可以看到其中…...

【电机控制】基于STC8H1K28的六步换向——方波驱动(软件篇)

【电机控制】基于STC8H1K28的六步换向——方波驱动&#xff08;软件篇&#xff09; 文章目录 [TOC](文章目录) 前言一、main.c二、GPIO.c三、PWMA.c四、ADC.c五、CMP.c六、Timer.c七、PMSM.c八、参考资料总结 前言 【电机控制】STC8H无感方波驱动—反电动势过零检测六步换向法 …...

小程序配置文件 —— 13 全局配置 - window配置

全局配置 - window配置 这里讲解根目录 app.json 中的 window 字段&#xff0c;window 字段用于设置小程序的状态栏、导航条、标题、窗口背景色&#xff1b; 状态栏&#xff1a;顶部位置&#xff0c;有网络信号、时间信息、电池信息等&#xff1b;导航条&#xff1a;有一个当…...

Creo 9.0新手必看:别再乱点‘基准平面’了,这7种创建方法才是正确打开方式

Creo 9.0基准平面实战指南&#xff1a;7种高效创建方法与避坑技巧 刚接触Creo 9.0的工程师们&#xff0c;是否经常遇到这样的场景&#xff1a;面对一个复杂零件建模时&#xff0c;明明脑子里已经构思好了结构&#xff0c;却卡在第一步——找不到合适的草绘平面&#xff1f;或者…...

从零打造专属显示器:面板、驱动板与外壳的实战选型指南

1. 为什么选择DIY显示器&#xff1f; 最近两年&#xff0c;显示器市场出现了不少高性价比的产品&#xff0c;但作为一个喜欢折腾的极客&#xff0c;我总觉得市面上的显示器少了点什么。要么是接口不够用&#xff0c;要么是外观太普通&#xff0c;要么就是某些参数达不到我的要求…...

Cadence 5141实战:手把手教你搞定Bandgap基准电压源电路(附完整仿真流程)

Cadence 5141实战&#xff1a;手把手教你搞定Bandgap基准电压源电路&#xff08;附完整仿真流程&#xff09; 在模拟集成电路设计中&#xff0c;基准电压源如同心脏般重要&#xff0c;而Bandgap电路则是这颗心脏的核心技术。无论你是微电子专业的学生&#xff0c;还是刚踏入模拟…...

人工智能系统的测试:AI模型的可靠性与鲁棒性测试

在人工智能技术深度渗透各行业的当下&#xff0c;AI模型的可靠性与鲁棒性直接关乎业务安全与用户信任。对于软件测试从业者而言&#xff0c;突破传统测试思维&#xff0c;构建适配AI特性的测试体系&#xff0c;已成为保障AI系统高质量落地的核心任务。 一、AI模型可靠性与鲁棒…...

保姆级教程:用Sen2Cor批量处理Sentinel-2 L1C到L2A(Win/Linux通用,附避坑清单)

遥感数据处理实战&#xff1a;Sen2Cor高效批量处理Sentinel-2 L1C至L2A全流程指南 当面对数百景Sentinel-2 L1C数据需要转换为L2A级别时&#xff0c;手动逐景处理不仅效率低下&#xff0c;还容易因操作失误导致数据不一致。本文将分享一套经过实际项目验证的批处理方案&#xf…...

CANN/hcomm集群信息初始化API

HcclCommInitClusterInfo 【免费下载链接】hcomm HCOMM&#xff08;Huawei Communication&#xff09;是HCCL的通信基础库&#xff0c;提供通信域以及通信资源的管理能力。 项目地址: https://gitcode.com/cann/hcomm 产品支持情况 Ascend 950PR/Ascend 950DT&#xff1…...

别再死记硬背公式了!用VisionMaster的N点标定,手把手教你搞定相机和机械手‘对齐’

视觉标定实战&#xff1a;用工具思维破解N点标定难题 在工业自动化领域&#xff0c;相机与机械手的协同工作就像两个语言不通的人试图完成精密舞蹈——标定就是为他们建立共同的坐标系词典。传统教材常将标定过程简化为数学公式的堆砌&#xff0c;导致许多工程师陷入"会推…...

从PyTorch到边缘设备:手把手教你用OpenVINO优化YOLOv5模型并在Jetson Orin上部署

从PyTorch到边缘设备&#xff1a;OpenVINO优化YOLOv5模型与Jetson Orin部署实战 在工业质检、智慧零售等实时场景中&#xff0c;将YOLOv5这类目标检测模型部署到Jetson Orin等边缘设备时&#xff0c;开发者常面临三大挑战&#xff1a;模型体积臃肿导致内存不足、计算资源有限影…...

告别杂音!在RK3588上搞定HDMI音频采集与实时播放的保姆级教程

告别杂音&#xff01;RK3588 HDMI音频采集与实时播放的终极调优指南 当你在RK3588开发板上调试HDMI音频采集时&#xff0c;是否曾被突如其来的"哒哒"声搞得焦头烂额&#xff1f;这种高频杂音不仅影响用户体验&#xff0c;更可能掩盖音频流的真实质量。本文将带你深入…...

终极指南:如何在Windows 11上轻松安装Android应用?APK Installer完整教程

终极指南&#xff1a;如何在Windows 11上轻松安装Android应用&#xff1f;APK Installer完整教程 【免费下载链接】APK-Installer An Android Application Installer for Windows 项目地址: https://gitcode.com/GitHub_Trending/ap/APK-Installer 你是否曾经想在Window…...