Build your first session with Netcode for GameObjects
Note: The Multiplayer Services SDK uses sessions to manage groups of players. Sessions relies internally on different combinations of Unity Gaming Services such Relay, Lobby, Matchmaker and Multiplay Hosting, and thus contributes to the billing of those services.
To start a multiplayer game, the host must create a session that other players join to participate in the game. A session represents a group of players that are connected together for a set period of time during a multiplayer game.
The best way to demonstrate the principles of Multiplayer Services sessions is the following example of a simple multiplayer game of moving capsules, one capsule controlled by the session's host, the other capsule controlled by the joining player. This guide helps you add the necessary game objects and code to add session management to your game.
Note: This example assumes that you have installed the packages listed in Get started.
Configure Multiplayer Play Mode
- In the Unity Editor, go to Window > Multiplayer Play Mode. This opens the Multiplayer Play Mode window.
- Enable Player 2 to test the project with a second window, and interact between them as different players.
Set up Netcode for GameObjects
Note: If you are using Netcode for Entities, refer to its instructions instead.
- In the Unity Editor, select GameObject > Create Empty to create a new GameObject in your scene.
- Rename it
NetworkManager
. - Select this NetworkManager you just created.
- Select Component > Netcode > Network Manager to attach a Network Manager component.
- In the Inspector window, go to Unity Transport > Protocol type, and select Unity Transport.
Create client network transform
Netcode for Game Objects assigns authority of every networked object to the server (or host) by default. The following script overrides this behavior for player prefabs and gives authority to the owning client, greatly improving the reactiveness of player movement, especially in poorer network conditions.
- In the Project tab, right-click the Assets folder, then Create > Folder.
- Name the new folder
Scripts
. - Right-click the new Scripts folder, then Create > Scripting > Empty C# Script.
- Rename this script
ClientNetworkTransform.cs
- Replace the contents of this script with the following code:
using UnityEngine; using Unity.Netcode.Components; [DisallowMultipleComponent] // Prevents you from adding the ClientNetworkTransform to the object more than once. public class ClientNetworkTransform : NetworkTransform { /// <summary> /// Used to determine who can write to this transform. Owner client only. /// This imposes state to the server. This is putting trust on your clients. Make sure no security-sensitive features use this transform. /// </summary> protected override bool OnIsServerAuthoritative() { return false; } }
Create the player Prefab
- In the Unity Editor, go to Edit > Project Settings > Player > Other Settings > Configuration > Active Input Handling, then select Both. Changing the input handling might trigger a restart of your Unity Editor.
- Close Project Settings.
- Right-click within the Hierarchy window, then select 3D Object > Capsule.
- Rename the capsule to
Player Prefab
. - With this capsule selected, in the Inspector window, select Add Component > Netcode > Network Object.
- In the Project tab, right-click the Scripts folder, then Create > Scripting > Empty C# Script.
- Rename this script
PlayerController.cs
. - Replace the contents of this script with the following code:
using UnityEngine; using Unity.Netcode; public class PlayerController : NetworkBehaviour { public float speed = 20; private ClientNetworkTransform _transform; private void Start() { _transform = GetComponent<ClientNetworkTransform>(); } private void Update() { if (!IsOwner) return; var movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); _transform.transform.position += movement * speed * Time.deltaTime; } }
- With the capsule player selected in the Hierarchy window, attach the
PlayerController
script to its Inspector window. If you receive a prompt that Network Behaviors require a NetworkObject, select Yes to add one. - Still in the Inspector window, select Add Component.
- Search for
Client Network Transform
, then add it. - In the Project window, right-click Assets, then Create > Folder.
- Rename this folder
Prefabs
. Refer to Prefabs. - Make the
Player Prefab
object you created earlier into a Prefab by dragging it from the Hierarchy window into thePrefabs
folder. - Delete the player from the scene by right-clicking the capsule within the Scene window, then Delete.
Tip: You can remove the player GameObject from the scene because you assigned this network Prefab to the player Prefab property in the
NetworkManager
component. The library doesn't support defining a player object as an in-scene placed NetworkObject. - In the Hierarchy window, select
NetworkManager
. - In its Inspector window, locate the Player Prefab field.
- Drag the capsule player from the Project window into the Player Prefab field you located.
- In the Unity Editor, select File > Save.
Add your scene to the build
In Inspector window for NetworkManager
, locate the Enable Scene Management setting (enabled by default). When you enable it, the server can control which scenes load for the clients. However, you must add the current scene to the build to enter Play Mode.
To add the current scene to the build:
- In the Unity Editor, select File > Build Profiles.
- Select Open Scene List.
- Select Add Open Scenes.
- Close Build Profiles.
Create and join your first session with Widgets and Multiplayer Services
The following steps outline how to create and join your first session with Widgets and Multiplayer Services in the Unity Editor.
- Right-click in the Hierarchy window, then select Multiplayer Widgets > Create > Create Session.
Note: If you receive a TMP Importer prompt, select Import TMP Essentials.
- In the Hierarchy window, select Canvas > Create Session.
- In its Inspector window, set Rect Transform > Pos Y to
50
. - Right-click in the Hierarchy window, then select Multiplayer Widgets > Info > Show Session Code.
- Right-click in the Hierarchy window, then select Multiplayer Widgets > Join and Leave > Join Session By Code.
- In the Hierarchy window, select Canvas > Join Session By Code.
- In its Inspector window, set Rect Transform > Pos Y to
-100
. - In the Unity Editor, select File > Save to refresh the scene for Player 2.
- In the Toolbar, select Play to enter Play Mode.
Assuming that you have enabled Multiplayer Play Mode, you can now create a session in one of the Editor windows and join it using the displayed join code from the other Editor window.