(对于新项目,应使用 5.1 版中引入的新网络系统。此信息适用于使用旧网络系统的旧版项目。)
网络视图是通过网络来共享数据的主要组件。这些视图允许两种类型的网络通信:__状态同步 (State Synchronization)__ 和__远程过程调用 (Remote Procedure Calls)__。
网络视图会监视特定对象以检测更改。然后将这些更改共享给网络上的其他客户端,以确保所有客户端都了解状态更改。此概念称为_状态同步_,可在状态同步页面了解更多有关此概念的信息。
在某些情况下,您不希望承担在客户端之间同步状态的开销,例如,在发送新对象或重生玩家的位置时。由于此类事件很少发生,因此同步所涉及对象的状态是没有意义的。相反,可以使用_远程过程调用_来告诉客户端或服务器执行这样的操作。可以在 RPC 手册页找到关于远程过程调用的更多信息。
网络视图通过其 __NetworkViewID__(基本上就是在网络机器之间协商的唯一标识符)在网络中标识身份。此标识符表示为 128 位数字,但如果可能,在通过网络传输时会自动压缩到 16 位。
到达客户端的每个数据包都需要应用于 NetworkViewID 指定的特定网络视图。使用这种方式,Unity 可以找到正确的网络视图,解压缩数据,并将传入的数据包应用于网络视图的观察对象。
有关在 Editor 中使用网络视图的更多详细信息,请参阅网络视图组件参考页面。
如果使用 Network.Instantiate() 创建联网对象,则无需担心分配网络视图和自行适当分配它们的问题。全部都将在幕后自动完成。
但是,您可以使用 Network.AllocateViewID 手动设置每个网络视图的 NetworkViewID 值。脚本参考文档举例说明了如何使用 RPC 函数在每个客户端上手动实例化对象,然后使用 AllocateViewID 手动设置 NetworkViewID。
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.