Multiplayer Sessions Building Block
Implement Unity's multiplayer sessions in your project with this Building Block's pre-made UI elements.
Read time 6 minutesLast updated 13 days ago
Implement Unity's multiplayer sessions in your project with this Unity Building Block's pre-made UI elements. Learn what sessions are, how to set them up, how to debug them, and how to connect to Unity's Netcode packages.
Introduction
The Multiplayer Session Building Block helps you integrate Unity's multiplayer sessions into your project to connect players. This Building Block can serve as a starting point for your multiplayer projects, or as a quick integration of sessions throughout development.
This Building Block demonstrates how to do the following:
- Create and join a session through the Multiplayer Services SDK.
- Use the Quick Join API to quickly get players into any existing session.
- Use the Session Browsing API to list existing sessions and join a specific one.
- Use a JoinCode to share and join a specific session.
The Multiplayer sessions Building Block includes example scenes, UIToolkit UI assets, and runtime components to create, browse, and join sessions, and debug them with a SessionInfo panel. If you need gameplay synchronization, you can optionally add Netcode using Unity Netcode for GameObjects or Netcode for Entities.
Prerequisites
- Install the Unity Building Block - Multiplayer Session from the Unity Asset Store:.
- Link your Unity Editor project to a cloud project.
Initial setup
You can validate that the Building Block is installed correctly by opening one of the included example scenes.
- Open one of the Multiplayer scenes by navigating to Assets > Blocks > MultiplayerSession > Scenes.
- Select Play to enter Play mode.
You can now interact with the UI elements in the scene to create a new session.
Multiplayer sessions
A multiplayer session represents a group of connected players and provides an abstraction layer for managing multiplayer game states, including initial player connections, host election, players joining and leaving, and network connection establishment. Multiplayer sessions don't facilitate gameplay synchronization, but are compatible with either Netcode for GameObjects or Network for Entities, which can handle authoritative game state replication after players connect. For example, a typical connection flow might look like the following:
- Initialize Unity Services
- Authenticate player
- Create or join a session
- Connect to the right network server or host
- Load gameplay scene
Included resources
The Multiplayer Session Building Block contains the following scenes:
Scene | Description |
|---|---|
| JoinByBrowsing | Let players browse available sessions to join or create their own new session. Ideal for finding lobbies with specific settings or players. |
| JoinByCode | Let players create a session and share a simple join code that other players can use to join the session. Perfect for joining and inviting players to a specific session through copying a code. |
| QuickJoin | Let players join the first available session. Perfect for quickly joining any existing session when matching with specific rules doesn't matter. |
| QuickJoinDebug | Similar to QuickJoin but with an additional SessionInfo window with information about the session. Useful for debugging a session. |
JoinByBrowsing
SessionBrowser- : Specify a session name and create a session.
CreateSessionElement - : Displays the available sessions. Click to select a session.
SessionBrowserElement - : Refreshes the list of sessions from the
RefreshListButton.SessionBrowserElement - : Join a selected session from the list.
JoinButton
CurrentSession- : Displays the name of the watched session.
SessionNameLabel - : Displays the number of players in the watched session.
PlayerCountLabel - : Displays the player's own name.
PlayerNameLabel - : Displays the join code for the session so it can be copied.
CopySessionCodeElement - : Displays the list of players that have joined the session.
PlayerListView - : Select to leave the joined session.
LeaveSessionButton
JoinByCode
JoinSessionByCode- : Specify a session name and create a session.
CreateSessionElement - : Displays the join code for the session so it can be copied.
CopySessionCodeElement - : Enter a session code to join a session.
JoinSessionByCode
CurrentSession- : Displays the name of the watched session.
SessionNameLabel - : Displays the number of players in the watched session.
PlayerCountLabel - : Displays the player's own name.
PlayerNameLabel - : Displays the join code for the session so it can be copied.
CopySessionCodeElement - : Displays the list of players that have joined the session.
PlayerListView - : Select to leave the joined session.
LeaveSessionButton
QuickJoin
JoinSessionByQuickJoin- : Join a session with the Quick Join API.
QuickJoinButton
CurrentSession- : Displays the name of the watched session.
SessionNameLabel - : Displays the number of players in the watched session.
PlayerCountLabel - : Displays the player's own name.
PlayerNameLabel - : Displays the join code for the session so it can be copied.
CopySessionCodeElement - : Displays the list of players that have joined the session.
PlayerListView - : Select to leave the joined session.
LeaveSessionButton
QuickJoinDebug
JoinSessionByQuickJoin- : Join a session with the Quick Join API.
QuickJoinButton
SessionInfo- : Displays debugging information for an active session, similar to the Sessions Viewer.
SessionInfo
CurrentSession- : Displays the name of the watched session.
SessionNameLabel - : Displays the number of players in the watched session.
PlayerCountLabel - : Displays the player's own name.
PlayerNameLabel - : Displays the join code for the session so it can be copied.
CopySessionCodeElement - : Displays the list of players that have joined the session.
PlayerListView - : Select to leave the joined session.
LeaveSessionButton
Joining with multiple clients locally
Use Multiplayer Play Mode to test the setup with multiple players directly from the Unity Editor.
Add Netcode to a session
To integrate either Netcode for GameObjects or Netcode for Entities with your sessions, do the following:
-
Install the Netcode package of your choice using the Package Manager (Window > Package Management > Package Manager).
- Install either Unity Netcode for GameObjects or Netcode for Entities.
-
Enable Create Network Session in.
SessionSettings- In your ScriptableObject, locate and enable the Create Network Session option, which allows your sessions to be recognized and managed by your chosen Netcode solution.
SessionSettings
- In your
-
Change the Network Type in.
SessionSettings-
Adjust the Network Type within yourto match your intended network architecture. You can choose from the following:
SessionSettings- Relay: For cloud-based relay services.
- Distributed authority: For using Netcode for GameObjects' distributed authority.
- Direct: For direct connections using IP Address and Port.
-
To understand more about the different NetworkTypes, refer to the Network connection management.
Troubleshooting
Multiplayer Services not available
- Multiplayer Services have to be initialized. You can initialize your own scenes with default settings by adding a ServicesInitialization and PlayerAuthentication components.
Authentication failure
- Ensure you're signed into the Editor, and the Project ID is correct.
- Check service enablement, for example, Authentication and Lobby.
Can't list or join sessions
- Verify network connectivity and firewall rules.
- Check Lobby service status and quotas.
Join code invalid or expired
- Confirm the affected session is active and not at capacity.
- Regenerate the join code by recreating the session.
- Ensure Relay is enabled and regions are available.
- Validate that endpoints are set before starting netcode.
UI not showing data
- Check that each in your UXML has its
VisualElementset properly.SessionType