NetworkManager
The NetworkManager is a component that manages the networking elements of a multiplayer game. It integrates several useful features into one place, making the creation and execution of multiplayer games as simple as possible.
Key Features of NetworkManager:
- Game State Management
- Spawn Management
- Scene Management
Only one active NetworkManager can exist in each scene (singleton). Do not place the NetworkManager component on network game objects that have the NetworkIdentity component, as these objects will be disabled when the scene loads.
Managing Game State
A networked multiplayer game can run in one of the following three modes:
- Client: A standard player mode that connects to the host or server.
- Dedicated Server: A mode dedicated solely to hosting the game server.
- Host: A mode that runs both the client and server simultaneously.
Start Methods
To implement your own UI, use the following methods to start each mode:
- StartClient: Start in client mode.
- StartServer: Start in dedicated server mode.
- StartHost: Start in host mode.
Connecting to Worlds Service
To connect to the worlds service, make sure to set the Use Cloud variable in NetworkManager to 'true'.
Managing Spawns
NetworkManager can manage the spawn (network instantiation) of network game objects from prefabs.
Most games have a prefab to represent players, so the NetworkManager has a Player Prefab slot that must be assigned the player prefab. When the player prefab is set, a player game object is automatically created for each user in the game, both for the local player on the host server and remote players on clients.
Before assigning the player prefab to this field, ensure that the NetworkIdentity component is added to the prefab.
After assigning the Player Prefab:
- When the game starts as a host, the player game object is created.
- When the game stops, the player game object is removed.
- When another game client connects to localhost, a new player game object is created.
- When the client stops, the corresponding player game object is removed.
Setting Start Position
By default, NetworkManager creates the Player Prefab at a defined transform position and rotation. Use the Player Spawn Method property to control how the starting position is chosen with the NetworkStartPosition component:
- Random: A player is spawned at a randomly selected start position.
- Round Robin: The start position is selected sequentially from a predefined list.
If a custom start position is needed:
- You can access available NetworkStartPosition components through the NetworkManager.startPositions list.
- Use the NetworkManager.GetStartPosition helper method within the OnServerAddPlayer implementation to find the starting position.
Managing Scenes
Most games are composed of multiple scenes. The NetworkManager is designed to automatically manage scene states and scene transitions in a way that is appropriate for multiplayer games.
The NetworkManager Inspector has two scene slots:
- Offline Scene
- Online Scene
By assigning scene assets to these slots, network scene management is activated.
Scene Management Behavior:
- When the server or host starts, the Online Scene is loaded.
- This scene becomes the current network scene.
- All clients connecting to the server are instructed to load this scene.
- The name of this scene is stored in the networkSceneName property.
- When the network stops (either the server/host stops or the client disconnects), the Offline Scene is loaded.
You can change the scene while the game is active by calling ServerChangeScene:
- The scene will be changed for all currently connected clients.
- The networkSceneName will be updated, and new clients will load the new scene.
- When changing scenes, all game objects from the previous scene are removed.
- NetworkManager must persist across scenes. If it does not, the network connection will be lost when changing scenes.
- To achieve this, activate the Don't Destroy On Load checkbox in the inspector.:::