注意:已弃用 UNet,未来会将其从 Unity 中删除。一个新系统正在开发中。如需了解更多信息和后续步骤,请参阅此博客文章以及常见问题解答 (FAQ)。 |
Unity Multiplayer 系统中有两种类型的联网游戏对象:
在运行时动态创建的游戏对象
保存为场景一部分的游戏对象
在运行时动态创建的游戏对象使用多人游戏生成系统,并且实例化这些游戏对象的预制件必须在 Network Manager 的联网游戏对象预制件列表中注册。
但是,作为场景一部分保存的联网游戏对象(因此在加载场景时已存在于场景中)的处理方式有所不同。这些游戏对象在客户端和服务器上作为场景的一部分加载,并且在 Multiplayer 系统发送任何生成消息之前已在运行时存在。
加载场景时,在客户端和服务器上,场景中的所有联网游戏对象都处于禁用状态。然后,场景完全加载之后,Network Manager 会自动处理场景的联网游戏对象,将这些游戏对象全部注册(从而使它们在客户端之间同步),并启用它们,就好像它们是在运行时生成的一样。
在场景中保存联网游戏对象(而不是在场景加载后动态生成游戏对象)有一些好处:
当 Network Manager 生成联网的场景游戏对象时,这些游戏对象的行为类似于动态生成的游戏对象。Unity 向这些游戏对象发送更新和 ClientRPC 调用。
如果在客户端加入游戏之前在服务器上销毁了场景游戏对象,则永远不会在加入的新客户端上启用该场景游戏对象。
当客户端连接时,将针对服务器上已存在并且对该客户端可见的每个场景游戏对象,向该客户端发送 ObjectSpawnScene 生成消息。此消息会使客户端上的游戏对象被启用,并包含该游戏对象来自服务器的最新状态。这意味着在客户端上仅激活客户端可见且未在服务器上销毁的游戏对象。与常规的非场景游戏对象一样,当客户端加入游戏时,这些场景游戏对象以最新状态启动。
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.