The NetworkManager is acomponent for managing the network state of a multiplayer game. It is actuallyimplemented entirely using the HLAPI, so everything it does is available todevelopers
in other forms. However the NetworkManager wraps up a lot of usefulfunctionality into a single place and makes creating, running and debuggingmultiplayer games as simple as possible.
The NetworkManager can beused entirely without scripting. It has inspector controls in the editor thatallow configuration of all of its features. The NetworkManagerHUD supplies asimple, default
user interface at runtime that allows the network game to becontrolled by the user. For advanced uses, developers can derive a class fromNetworkManager and customize its behaviour by overriding any of the virtualfunction hooks that it provides.
The NetworkManager can beused as the core controlling component of a multiplayer game. To get started,create an empty game object in your starting scene, or pick a convenientmanager object.
Then add the NetworkManager component from the Network/NetworkManagermenu item. The newly added NetworkManager component should look something like:
The inspector for theNetworkManager in the editor allows you to configure and control many thingsrelated to networking.
The NetworkManagerHUD isanother component that works with the NetworkManager. It gives you a simpleuser interface when the game is running to control the network state. This isgood for getting
started with a network project, but not intended to be used asthe final UI for a game. The NetworkManagerHUD looks like:
A Networking multiplayer gamecan run in three modes - as a client, as a dedicated server, or as a “Host”which is both a client and a server at the same time. Networking is designed tomake
the same game code and assets work in all of these cases. Developing forthe single player version of the game and the multiplayer version of the gameshould be the same thing.
NetworkManager has methodsfor entering each of these modes. NetworkManager.StartClient(),NetworkManager.StartServer() and NetworkManager.StartHost() are all availableto script code, so they
can be invoked from keyboard input handlers or fromcustom user interfaces. The default runtime controls that can optionally bedisplayed also invoke these same functions. There are also buttons in theNetworkManagerHUD inspector available when in play mode that
call the samefunctions:
Whatever method is used tochange the game state, the properties networkAddress and networkPort are used.When a server or host is started, networkPort becomes the listen port. When aclient
is started, networkAddress is the address to connect to, and networkPortis the port to connect to.
The NetworkManager can beused to manage spawning of networked objects from prefabs. Most games have aprefab used as the main player object, so the NetworkManager has a slot to dragthe player
prefab. When a player prefab is set, a player object will automaticallybe spawned from that prefab for each user in the game. This applies to thelocal player on a hosted server, and remote players on remote clients. Notethat the player prefab must have the
NetworkIdentity component on it.
In addition to the playerprefab, the prefabs of other objects that will be dynamically spawned must beregistered with the ClientScene. This can be done with theClientScene.RegisterPrefab()
functions, or it can be done by the NetworkManagerautomatically. Adding prefabs to the spawn list will make them beauto-registered. The spawn configuration section of the NetworkManagerinspector looks like:
Once a player prefab is set,you should be able to start the game as a host and see the player object bespawned. Stopping the game should make the player object be destroyed. Runninganother
copy of the game and connecting as a client to localhost should makeanother player object appear, and stopping that client should make thatclient’s player object be destroyed.
The player object is spawnedby the default implementation of NetworkManager.OnServerAddPlayer. If you wantto customize the way player objects are created, you can override that virtualfunction.
The default implementation is something like:
Note that the functionNetworkServer.AddPlayerForConnection() must be called for the newly createdplayer object so that it is spawned and associated with the client’s connection.This will
Spawn the object, so NetworkServer.Spawn does not need to be calledfor the player object.
To control where players arespawned, you can use the NetworkStartPosition component. The NetworkManagerlooks for NetworkStartPosition objects in a scene, and if it finds any, then itwill
spawn player at the position and orientation of one of them. Custom codecan access the available NetworkStartPositions by the listNetworkManager.startPositions, and there is also a helper functionGetStartPosition() on the NetworkManager that can be used in
implementation ofOnServerAddPlayer to find a start position.
To use start positions,attach a NetworkStartPosition component to an object in the play scene. Therecan be multiple start positions within a scene. The NetworkManager will thenregister the
position and orientation of the object as a start position. When aclient joins the game and a player is added, the player object will be createdat one of the start positions, with the same position and orientation.
Most games have more than onescene. At very least there is usually a title screen or starting menu scene inaddition to the scene where the game is actually played. The NetworkManager issetup
to automatically manage scene state and scene transitions in a way thatworks for a multiplayer game. There are two slots on the NetworkManagerinspector, the offlineScene and the onlineScene. Dragging scene objects intothese slots activates networked scene
When a server or host isstarted, the online scene will be loaded. This will then become the currentnetwork scene. Any clients that connect to that server will be instructed toalso load that
scene. The name of this scene is stored in the networkSceneNameproperty.
When the network is stopped,by stopping the server or host, or by a client disconnecting, the offline scenewill be loaded. This allows the game to automatically return to a menu scenewhen
disconnected from a multiplayer game.
You can also change sceneswhile the game is active by calling NetworkManager.ServerChangeScene(). Thiswill make all the currently connected clients change scene too, and will updatenetworkSceneName
so that new clients will also load the new scene.
While networked scenemanagement is active, any calls to game state managerment functions suchNetworkManager.StartHost() or NetworkManager.StopClient() can cause scenechanges. This applies
to the runtime control UI. So by setting up scenes andcalling these functions it is easy to control the flow of a multiplayer game.
Note that scene changes causeall the objects in the scene to be destroyed. This could include theNetworkManager! If you want the NetworkManager to persist between scenes, thenensure the “Dont
Destroy On Load” checked boxed is set to true. In simplecases, this is the best configuration. But, it is possible to have aNetworkManager in each scene with different settings to control incrementalprefab loading, or different scene transitions.
Also, registered clientmessage handlers are shown in the preview window.
The NetworkManager runtime UIand NetworkManager inspector UI allow interactions with the matchmaker service.The function NetworkManager.StartMatchmaker() enables matchmaking, andpopulates
the NetworkManager.matchmaker property with a NetworkMatch object.Once this is active, the default UIs use it and callback functions onNetworkManager to let you perform simple matchmaking.
There are virtual functionson NetworkManager that derived classes can use to customize behaviour ofresponding to matchmaker callbacks.
There are virtual functionson NetworkManager that derived classes can use to customize behaviour. When implementing these functions, be sure to take care of the functionality that the default
implementations provide. For example, in OnServerAddPlayer(), thefunction NetworkServer.AddPlayer must be called to active the player object forthe connection.