前言
没看过前面的教程请先阅读前面的教程,本期将会讲到Netcode联机的申请,当一个Client想连接进来,应向Server发送申请联机的信息,然后由服务端向客户端下发数据,所有数据应该在服务端,而不是在客户端。
适用场景
举几个常见的例子需要用到ConnectionApproval
的场景
- 使用密码加入房间
- 玩家选择不同的角色
- 房间人数
- 各种需要服务端下发的信息等等
1. 准备
需要在第二期上添加多的一个Player
预制体
每个Player
都需要挂载上NetworkObject
这个组件
最重要的一点是在
NetworkManager
上开启ConnectionApproval
2.新建GameManager
新建一个GameManager
空物体,添加NetworkObject
这个组件
在Scripts
目录下新建一个GameManager
脚本,挂载到GameManager
的空物体上
using UnityEngine;
using Unity.Netcode;
using System.Text;
public class GameManager : NetworkBehaviour
{
public GameObject Man;
public GameObject Girl;
public override void OnNetworkSpawn()
{
if (IsServer)
{
NetworkManager.ConnectionApprovalCallback = ApprovalCheck;
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.O))
{
NetworkManager.Singleton.NetworkConfig.PlayerPrefab = Man;
NetworkManager.Singleton.StartHost();
}
if (Input.GetKeyDown(KeyCode.P))
{
NetworkManager.Singleton.NetworkConfig.ConnectionData = Encoding.UTF8.GetBytes("Girl");
NetworkManager.Singleton.StartClient();
}
}
private void ApprovalCheck(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
{
// The client identifier to be authenticated
var clientId = request.ClientNetworkId;
// Additional connection data defined by user code
var connectionData = request.Payload;
var _character = Encoding.UTF8.GetString(connectionData);
if (_character == "Girl")
{
response.PlayerPrefabHash = Girl.GetComponent<NetworkObject>().PrefabIdHash;
}
else
{
response.PlayerPrefabHash = Man.GetComponent<NetworkObject>().PrefabIdHash;
}
// Your approval logic determines the following values
response.Approved = true;
response.CreatePlayerObject = true;
// Position to spawn the player object (if null it uses default of Vector3.zero)
response.Position = Vector3.zero;
// Rotation to spawn the player object (if null it uses the default of Quaternion.identity)
response.Rotation = Quaternion.identity;
// If response.Approved is false, you can provide a message that explains the reason why via ConnectionApprovalResponse.Reason
// On the client-side, NetworkManager.DisconnectReason will be populated with this message via DisconnectReasonMessage
response.Reason = "Some reason for not approving the client";
// If additional approval steps are needed, set this to true until the additional steps are complete
// once it transitions from true to false the connection approval response will be processed.
response.Pending = false;
}
}
在Inspector
窗口绑定一下两个角色到脚本中
3.编译运行
分别按下O
键与P
键,可以看到有两个不同的Player Prefab
实例化了。
4.脚本详解
GameManager
继承的是NetworkBehaviour
而非MonoBehaviour
重写OnNetworkSpawn()
方法,让NetworkManager
放入回调方法ApprovalCheck
让服务端处理
public override void OnNetworkSpawn()
{
if (IsServer)
{
NetworkManager.ConnectionApprovalCallback = ApprovalCheck;
}
}
ApprovalCheck
这个回调方法传入两个类型的参数,分别是NetworkManager.ConnectionApprovalRequest
和NetworkManager.ConnectionApprovalResponse
NetworkManager.ConnectionApprovalRequest
是客户端的请求,一共两个字段
–ClientNetworkId
是客户端的唯一标识ID
–Payload
是客户端发送的信息,封装成byte
类型,这也是我们主要使用的
private void ApprovalCheck(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
{
// The client identifier to be authenticated
var clientId = request.ClientNetworkId;
// Additional connection data defined by user code
var connectionData = request.Payload;
var _character = Encoding.UTF8.GetString(connectionData);
if (_character == "Girl")
{
response.PlayerPrefabHash = Girl.GetComponent<NetworkObject>().PrefabIdHash;
}
else
{
response.PlayerPrefabHash = Man.GetComponent<NetworkObject>().PrefabIdHash;
}
// Your approval logic determines the following values
response.Approved = true;
response.CreatePlayerObject = true;
// Position to spawn the player object (if null it uses default of Vector3.zero)
response.Position = Vector3.zero;
// Rotation to spawn the player object (if null it uses the default of Quaternion.identity)
response.Rotation = Quaternion.identity;
// If response.Approved is false, you can provide a message that explains the reason why via ConnectionApprovalResponse.Reason
// On the client-side, NetworkManager.DisconnectReason will be populated with this message via DisconnectReasonMessage
response.Reason = "Some reason for not approving the client";
// If additional approval steps are needed, set this to true until the additional steps are complete
// once it transitions from true to false the connection approval response will be processed.
response.Pending = false;
}
NetworkManager.ConnectionApprovalResponse
是服务端给客户端的响应,一共有八个字段
–Approved
:是否同意客户端加入
–Reason
: 如果Approved
为false
,填写拒绝理由
–CreatePlayerObject
是否生成玩家实体
–PlayerPrefabHash
如果null
则生成在NetworkManager
默认的Player Prefab
,这个值要填入NetworkObject
的PrefabIdHash
–Position
生成玩家实体的position
–Rotation
生成玩家实体的rotation
–Pending
挂起approval
延迟授权,直到其他客户端完成approval
, 官方解释:Provides the ability to mark the approval as pending to delay the authorization until other user-specific code finishes the approval process.
后话
官方相关的阅读文档: