Network Manager HUD
Network Proximity Checker

Network Manager

Network Manager 组件用于控制联网游戏的状态。此组件在 Editor 中提供了用于配置网络的界面、用于生成游戏对象的预制件以及用于不同游戏状态的场景。

有关在游戏中实现 Network Manager 的更多详细信息,请参阅关于使用 Network Manager 的文档。

Inspector 窗口中的 Network Manager 组件
Inspector 窗口中的 Network Manager 组件
属性 功能
Dont Destroy On Load 使用此属性可控制在场景更改时 Unity 是否应使用 Network Manager 销毁游戏对象。勾选此复选框可确保当游戏中场景发生变化时 Unity 不会销毁 Network Manager 游戏对象。如果希望 Unity 在游戏对象所在的场景不再是活动场景时销毁游戏对象,请取消选中该复选框。如果要在每个场景中管理多个单独的 Network Manager 游戏对象,这非常有用。默认情况下会勾选此复选框。
Run In Background 使用此属性可控制当运行联网游戏的窗口未获得焦点时是否运行游戏。如果希望当运行游戏的窗口未获得焦点时停止运行游戏,请勾选此复选框。默认情况下会勾选此复选框。如果要在同一台机器上运行多个程序实例(例如使用 localhost 进行测试时),则需要启用此属性。在部署到移动平台时,应禁用此属性。启用此属性后会在 Network Manager 启动时将 Application.runInBackground 设置为 true。还可以从 Unity 菜单 (Edit > Project Settings > Player Settings > Resolution & Presentation) 中设置此属性。
Log Level 使用此属性可控制 Unity 输出到控制台窗口的信息量。级别越低,产生信息的越多;级别越高,产生的信息越少。每个级别包括来自高于该级别的所有级别的消息(例如,如果选择“Warn”,则控制台还会打印输出所有“Error”和“Fatal”日志消息)。下拉选单按从低到高顺序列出这些级别。默认情况下,此属性设置为 Info。可将 Log Level 设置为 Set in Scripting 来完全阻止 Network Manager 设置日志级别。这意味着可以从您自己的脚本控制级别。
Offline Scene 如果将场景分配给此字段,Network Manager 会在网络会话停止时自动切换到指定的场景;例如,当客户端断开连接时,或服务器关闭时。
Online Scene 如果将场景分配给此字段,Network Manager 会在网络会话开始时自动切换到指定的场景;例如,当客户端连接到服务器时,或者服务器开始监听连接时。
Network Info You can expand this section of the inspector to access network-related settings, listed below
Use Web Sockets When running as a host, enable this setting to make the host listen for WebSocket connections instead of normal transport layer connections, so that WebGL clients can connect to it (if you build your game for the WebGL platform). These WebGL instances of your game cannot act as a host (in either peer-hosted or server-only mode). Therefore, for WebGL instances of your multiplayer game to be able to find each other and play together, you must host a server-only instance of your game running in LAN mode, with a publicly reachable IP address, and it must have this option enabled. This checkbox is unticked by default.
Network Address The network address currently in use. For clients, this is the address of the server that is connected to. For servers, this is the local address. This is set to ‘localhost’ by default.
Network Port The network port currently in use. For clients, this is the port of the server connected to. For servers, this is the listen port. This is set to 7777 by default.
Server Bind To IP Allows you to tell the server whether to bind to a specific IP address. If this checkbox is not ticked, then there is no specific IP address bound to (IP_ANY). This checkbox is unticked by default. Use this if your server has multiple network addresses (eg, internal LAN, external internet, VPN) and you want to specific the IP address to serve your game on.
Server Bind Address This field is only visible when the Server Bind To IP checkbox is ticked. Use this to enter the specific IP address that the server should bind to.
Script CRC Check When this is enabled, Unity checks that the clients and the server are using matching scripts. This is useful to make sure outdated versions of your client are not connecting to the latest (updated) version of your server. This checkbox is ticked by default. It does this by performing a (CRC check)[https://en.wikipedia.org/wiki/Cyclic_redundancy_check] between the server and client that ensures the NetworkBehaviour scripts match. This may not be appropriate in some cases, such as when you are intentionally using different Unity projects for the client and server. In most other cases however, you should leave it enabled.
Max Delay The maximum time in seconds to delay buffered messages. The default of 0.01 seconds means packets are delayed at most by 10 milliseconds. Setting this to zero disables HLAPI connection buffering. This is set to 0.01 by default.
Max Buffered Packets The maximum number of packets that a NetworkConnection can buffer for each channel. This corresponds to the ChannelOption.MaxPendingBuffers channel option. This is set to 16 by default.
Packet Fragmentation This allows the NetworkConnection instances to fragment packets that are larger than maxPacketSize, to up a maximum of 64K. This can cause delays in sending large packets. This checkbox is ticked by default.
MatchMaker Host URI The host address for the MatchMaker server. By default this points to the global Unity Multiplayer Service at mm.unet.unity3d.com, and usually you should not need to change this. Unity automatically groups players of your game into regional servers around the world, which ensures fast multiplayer response times between players in the same region. This means, for example, that players from Europe, the US, and Asia generally end up playing with other players from their same global region. You can override this value to explicitly control which regional server your game connects to. You might want to do this via scripting if you want to give your players the option of joining a server outside of their global region. For example, if “Player A” (in the US) wanted to connect to a match created via matchmaker by “Player B” (in Europe), they would need to be able to set their desired global region in your game. Therefore you would need to write a UI feature which allows them to select this. See API reference documentation on NetworkMatch.baseUri for more information, and for the regional server URIs.
MatchMaker Port The host port for the Matchmaker server. By default this points to port 443, and usually you should not need to change this.
Match Name Define the name of the current match. This is set to “default” by default.
Maximum Match Size Define the maximum number of players in the current match. This is set to 4 by default.
SpawnInfo You can expand this section of the inspector to access spawn-related settings, listed below
Player Prefab Define the default prefab Unity should use to create player GameObjects on the server. Unity creates Player GameObjects in the default handler for AddPlayer on the server. Implement (OnServerAddPlayer)[https://docs.unity3d.com/ScriptReference/Networking.NetworkManager.OnServerAddPlayer.html] to override this behaviour.
Auto Create Player Tick this checkbox if you want Unity to automatically create player GameObjects on connect, and when the Scene changes. This checkbox is ticked by default. Note that if you are using the MigrationManager and you do not enable Auto Create Player, you need to call ClientScene.SendReconnectMessage when your client reconnects.
Player Spawn Method Define how Unity should decide where to spawn new player GameObjects. This is set to Random by default.
    Random Choose Random to spawn players at randomly chosen startPositions.
    Round Robin Choose Round Robin to cycle through startPositions in a set list.
Registered Spawnable Prefabs Use this list to add prefabs that you want the Network Manager to be aware of, so that it can spawn them. You can also add and remove them via scripting.
Advanced Configuration Tick this checkbox to reveal advanced configuration options in the Network Manager Inspector window.
    Max Connections Define the maximum number of concurrent network connections to support. This is set to 4 by default.
    Qos Channels A list containing the different communication channels the current Network Manager has, and the Quality Of Service (QoS) setting for each channel. Use this list to add or remove channels, and adjust their QoS setting. You can also configure the channels via scripting. For the descriptions of each QoS option, see QosType.
    Timeouts
        Min Update Timeout Set the minimum time (in milliseconds) the network thread waits between sending network messages. The network thread doesn’t send multiplayer network messages immediately. Instead, it check each connection periodically at a fixed rate to see if it has something to send. This is set to 10ms by default. See API reference documentation on MinUpdateTimeout for more information.
        Connect Timeout Define the amount of time (in milliseconds) Unity should wait while trying to connect before attempting the connection again. This is set to 2000ms by default. See API reference documentation on ConnectTimeout for more information.
        Disconnect Timeout The amount of time (in milliseconds) before Unity considers a connection to be disconnected. This is set to 2000ms by default. See API reference documentation on DisconnectTimeout for more information.
        Ping Timeout The amount of time (in milliseconds) between sending pings (also known as “keep-alive” packets). The ping timeout duration should be approximately one-third to one-quarter of the Disconnect Timeout duration, so that Unity doesn’t assume that clients are disconnected until the server has failed to receive at least three pings from the client. This is set to 500ms by default. See API reference documentation on ConnectionConfig.PingTimeout for more information.
    Global Config These settings relate to the Reactor. The Reactor is the part of the multiplayer system which receives network packets from the underlying operating system, and passes them into the multiplayer system for processing.
        Thread Awake Timeout The timeout duration in milliseconds, used by the Reactor. How the Reactor uses this value depends on which Reactor Model you select (see below). This is set to 1ms by default.
        Reactor Model Choose which type of reactor to use. The reactor model defines how Unity reads incoming packets. For most games and applications, the default Select reactor is appropriate. If you want to trade a small delay in the processing of network messages for lower CPU usage and improved battery life, use the Fix Rate reactor.
        Select Reactor This model uses the select() API which means that the network thread “awakens” (becomes active) as soon as a packet is available. Using this method means your game gets the data as fast as possible. This is the default Reactor Model setting.
        Fix Rate Reactor This model lets the network thread sleep manually for a given amount of time (defined by the value in Thread Awake Timeout) before checking whether there are incoming packets waiting to be processed.
        Reactor Max Recv Messages Set the maximum number of messages stored in the receive queue. This is set to 1024 messages by default.
        Reactor Max Sent Messages Set the maximum number of messages stored in the send queue. This is set to 1024 messages by default.
Use Network Simulator Tick this checkbox to enable the usage of the network simulator. The network simulator introduces simulated latency and packet loss based on the following settings:
    Simulated Average Latency The amount of delay in milliseconds to simulate.
    Simulated Packet Loss The amount of packet loss to simulate in percent.
Network Manager HUD
Network Proximity Checker