Note: UNet is deprecated, and will be removed from Unity in the future. A new system is under development. For more information and next steps see this blog post. |
服务器和客户端都可以管理游戏对象的行为。“授权”的概念是指如何以及在何处管理游戏对象。
使用 HLAPI 的 Unity 联网游戏中的默认授权状态是:服务器对所有不代表玩家的游戏对象都具有授权。举例来说,这意味着服务器将管理所有可收集物品、移动平台、NPC 以及玩家可以交互的游戏任何其他部分的控制权,并且玩家游戏对象对其所有者的客户端具有授权(意味着客户端管理它们的行为)。
本地授权(有时称为客户端授权)表示本地客户端具有对特定联网游戏对象的授权控制。这与默认状态形成对比,默认状态是服务器具有对联网游戏对象的授权控制。
除了 isLocalPlayer**
** 之外,还可以选择让玩家游戏对象拥有“本地授权”。这意味着其所有者客户端上的玩家游戏对象对其自身负责(或具有授权)。这对于控制移动特别有用;这意味着每个客户端对于自身玩家游戏对象如何受到控制都具有授权。
要在游戏对象上启用本地玩家授权,请勾选 Network Identity 组件的 Local Player Authority 复选框。Network Transform 组件会使用此“授权”设置,并将移动信息从客户端发送到其他客户端(如果已进行此设置)。
有关通过脚本实现本地玩家授权的信息,请参阅关于 NetworkIdentity 和 localPlayerAuthority 的脚本 API 参考文档。
使用 NetworkIdentity.hasAuthority 属性可确定游戏对象是否具有本地授权(为方便起见,也可在 NetworkBehaviour
上访问)。非玩家游戏对象在服务器上具有授权,而设置了 localPlayerAuthority 的玩家游戏对象在其所有者的客户端上具有授权。
针对非玩家游戏对象可以获取其客户端授权。有两种方式可以做到这一点。一种是使用 NetworkServer.SpawnWithClientAuthority 生成游戏对象,并传递客户端的网络连接以获得所有权。另一种是使用 NetworkIdentity.AssignClientAuthority 与客户端的网络连接来获取所有权。
为客户端分配授权会导致 Unity 在游戏对象上的每个 NetworkBehaviour
上调用 OnStartAuthority(),并将 hasAuthority
** 属性设置为 true。在其他客户端上,hasAuthority
**属性仍为 false。具有客户端授权的非玩家游戏对象可以像玩家一样发送命令。这些命令对服务器的游戏对象实例运行,而不是对与连接相关联的玩家运行。
如果希望非玩家游戏对象具有客户端授权,则必须在这些游戏对象的 Network Identity 组件上启用 localPlayerAuthority。下面的示例将生成一个游戏对象并将授权分配给生成此游戏对象的玩家的客户端。
[Command]
void CmdSpawn()
{
var go = (GameObject)Instantiate(otherPrefab, transform.position + new Vector3(0,1,0), Quaternion.identity);
NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
}
NetworkBehaviour
类包含的属性允许脚本随时了解联网游戏对象的上下文。
isServer - 如果游戏对象位于服务器(或主机)上并且已生成,则为 true。
isClient - 如果游戏对象位于客户端上并且是由服务器创建的,则为 true。
isLocalPlayer - 如果游戏对象是此客户端的玩家游戏对象,则为 true。
hasAuthority - 如果游戏对象归本地进程所有,则为 true
要查看这些属性,请选择要检查的游戏对象,然后在 Inspector 窗口中查看 NetworkBehaviour 脚本组件的预览窗口。可以使用这些属性的值来根据运行脚本的上下文执行代码。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.