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. |
联网游戏对象是由 Unity 网络系统控制和同步的游戏对象。通过使用同步的联网游戏对象,可为正在玩游戏的所有玩家创建一种共享体验。尽管玩家可能处于游戏中的不同视角,但他们将看到和听到相同的事件和操作。
Unity 中的多人游戏所构建的场景通常会混合联网游戏对象和常规(非联网)游戏对象。联网游戏对象表示游戏对象在游戏过程中的移动或变化信息需要在该游戏的所有玩家之间同步。非联网游戏对象表示在游戏过程中完全不会移动或变化的游戏对象(例如岩石或栅栏之类的静态障碍物),或者游戏对象的移动或变化不需要在玩家之间同步(例如,轻轻摇曳的树木或在游戏背景中掠过的云彩)。
联网游戏对象附加了 Network Identity 组件。但是,仅包含 Network Identity 组件不足以让游戏对象在多人游戏中正常运行。Network Identity 组件是同步的起点,允许 Network Manager 同步游戏对象的创建和销毁,但除此之外,不会指定游戏对象的哪些属性应该同步。
在每个联网游戏对象上应该具体同步哪些内容取决于所制作的游戏类型,以及每个游戏对象的用途。例如,可能需要同步的内容如下:
移动游戏对象(如玩家和非玩家角色)的位置和旋转。
动画游戏对象的动画状态
变量的值,例如在当前一轮游戏中剩余多少时间,或者玩家拥有多少能量。
有些属性可由 Unity 自动同步。联网游戏对象的同步创建和销毁(称为生成)由 NetworkManager 管理。可以使用 Network Transform 组件来同步游戏对象的位置和旋转,还可以使用 Network Animator 组件来同步游戏对象的动画。
为了同步联网游戏对象的其他属性,需要使用脚本。有关此方面的更多信息,请参阅状态同步。
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.