网站开发不足之处,济南定制网页制作,购物网站 备案,青岛seo服务文章目录 前言Basic示例场景元素预制体元素代码逻辑BasicNetManagerPlayer逻辑SyncVars属性Server逻辑Client逻辑 PlayerUI逻辑 最后 前言
在现代游戏开发中#xff0c;网络功能日益成为提升游戏体验的关键组成部分。Mirror是一个用于Unity的开源网络框架#xff0c;专为多人… 文章目录 前言Basic示例场景元素预制体元素代码逻辑BasicNetManagerPlayer逻辑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);}#endregionServer逻辑
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./// paraThis could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created./para/// paraThis will be called for objects on a host as well as for object on a dedicated server./para/// /summarypublic 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/// paraUseful for saving object data in persistent storage/para/// /summarypublic override void OnStopServer(){CancelInvoke();playersList.Remove(this);}#endregionClient逻辑
Client逻辑就是纯粹的客户端显示逻辑。 比如: OnStartClient.,当玩家在客户端激活时创建一个指定的UI显示玩家信息 OnStartLocalPlayer当本机玩家连接成功后显示画布UI代表自己登录成功了 #region Client/// summary/// Called on every NetworkBehaviour when it is activated on a client./// paraObjects 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/// /summarypublic override void OnStartClient(){// Instantiate the player UI as child of the Players PanelplayerUIObject Instantiate(playerUIPrefab, CanvasUI.GetPlayersPanel());playerUI playerUIObject.GetComponentPlayerUI();// 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./// paraThis 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/// /summarypublic 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./// paraThis 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/// /summarypublic 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./// paraThis can be used as a hook to invoke effects or do client specific cleanup./para/// /summarypublic override void OnStopClient(){// disconnect event handlersOnPlayerNumberChanged null;OnPlayerColorChanged null;OnPlayerDataChanged null;// Remove this players UI objectDestroy(playerUIObject);}#endregionPlayerUI逻辑
PlayerUI比较简单当Player属性发行变化时会调用对应的函数更新PlayerUI显示。
Player中有对应的事件绑定 // Instantiate the player UI as child of the Players PanelplayerUIObject Instantiate(playerUIPrefab, CanvasUI.GetPlayersPanel());playerUI playerUIObject.GetComponentPlayerUI();// 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中做玩家的位移等。 好了这篇文章就到这里希望对你有所帮助