NetworkServer 使用 NetworkServerSimple 实现基本网络功能,并增加了更多类似游戏的功能。
NetworkServer 通过 NetworkServerSimple 实例处理来自远程客户端的远程连接,并且拥有针对本地客户端的本地连接。
NetworkServer 是一个单例。它提供静态的便捷函数,例如 NetworkServer.SendToAll() 和 NetworkServer.Spawn(),这些函数自动使用单例实例。
NetworkServer 可以与 NetworkManager 结合使用,但也可以独立使用。
生成的联网对象集由 NetworkServer 管理。NetworkServer.Spawn() 生成对象并将其添加到该集合中,并在客户端上创建这些对象。生成的对象被销毁时,系统会自动将其删除,或者,也可以通过调用 NetworkServer.UnSpawn() 从生成的对象集中将其删除,但这不会销毁这些对象。
NetworkServer 会使用许多内部消息,这些消息是在调用 NetworkServer.Listen() 时设置的。
AddPlayerForConnection | 当 AddPlayer 消息处理程序收到玩家请求后,服务器会调用此请求,以将玩家对象与连接关联。 |
BecomeHost | 这将允许已从服务器断开连接的客户端成为新版游戏的主机。 |
ReplacePlayerForConnection | 此方法会将连接的玩家对象替换成其他玩家对象。不销毁旧玩家对象。 |
SendBytesToReady | 此方法会向所有准备的玩家发送一个字节数组。 |
SendToAll | 将带有指定类型编号的消息结构发送到所有连接的客户端。 |
SendToClient | 将消息发送到拥有指定连接 ID 的客户端。 |
SendToClientOfPlayer | 将消息发送到拥有指定玩家对象实例的客户端。 |
SendToReady | 将带有指定类型编号的消息结构仅发送到已准备就绪的客户端。 |
SendUnreliableToAll | 将指定消息结构作为不可靠消息发送到所有连接的客户端。 |
SendUnreliableToReady | 将指定消息结构作为不可靠消息仅发送到已准备就绪的客户端。 |
SendWriterToReady | 将 NetworkWriter 对象的内容发送至已准备就绪的玩家。 |
Spawn | 在已准备就绪的所有客户端上生成指定的游戏对象。 |
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.