Skip to main content

Network Manager

Component

The NetworkManagerCustom component is your first stop for methods that involve finding, starting or joining a match. However, it bundles all logic within a single static method: StartMatch(NetworkMode mode). Network Modes are explained in more detail in their own section. The NetworkManager prefab should be placed into your first scene.

NetworkManager010

On the NetworkManagerCustom script inspector, you can define an important network setting, specifically the maximum count of allowed connections = maximum players in a match. Additionality, the player prefabs you are going to use for your game need to be assigned in the Player Prefabs array and the NetworkPrefabs list. Other networked prefabs also have to be put in there, even though some of them are being handled exclusively by our PoolManager during the game.

Further down in the components list is the UnityTransport component, which is used by NetworkManagerCustom and describes the raw transport protocol used for network messages.

Player Data

Since the player can customize quite a few things in the game settings, including the selected player model and user name, the server needs to be aware of these values in order to correctly spawn the player. We call this Player Data. Let's go over both values in more detail:

Player model: In the shop, the IAPProduct component for each model has a unique value assigned to identify which model has been selected by the player at runtime. This value is matched against the Player Prefabs list on the NetworkManager component, so 0 resembles the first model, 1 = the second and so on.

NetworkManager200

User name: Regarding input and storage, this is not something special and just a regular InputField which is saved locally. The more interesting part, how it is sent to the server, is explained below.

Since we do not store user inventories online, when joining a game the client needs to tell the server its player model and user name.

These values are first read from PlayerPrefs, put in a JoinMessage struct and serialized into a byte array. All of this happens in the GetJoinMessage() method. Then, when the client is connected to the server and loaded into a game scene, it calls the RPC AddPlayerServerRpc() for sending its Player Data to the server.