Join a session
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.
There are multiple ways a player can join a previously-created session. Players can join by:
Join code
await MultiplayerService.Instance.JoinSessionByCodeAsync(joinCode);
A unique and human-friendly join code is generated when a host starts a session. A host can then share this join code with other players to invite them to play the same session. The codes use a combination of letters and numbers and are designed to be short and easy to type or spell out loud.
Refer to JoinSessionByCodeAsync.
Browsing sessions in a list
You can use a session query to list sessions.
await MultiplayerService.Instance.QuerySessionsAsync(queryOptions);
Query results include all non-full, public sessions a player can join. The public properties of the session, such as its name, can then present a list to the player. The player can then select a session to join directly by ID.
Note: You can use queries to discover a session as long as its IsPrivate property is set to false. A session's ID is not the same as its join code.
Reconnect to a session
A user can reconnect to a session while they remain a member of that session (for example, after a quick disconnection).
Note: Reconnection was designed specifically for when a user has not yet been removed from the session (either by the host or by the service). If the player has already been removed, join the session again.
To reconnect a user:
Fetch all the sessions the current user is part of using GetJoinedSessionIdsAsync:
await MultiplayerService.Instance.GetJoinedSessionIdsAsync();
Note: If a session type was provided when creating and/or joining the session, you have to save the mapping between the session ID and the session type. The Multiplayer SDK does not save this information.
To reconnect to the chosen session, call ReconnectAsync:
await sessionManager.ReconnectAsync(sessionId);
You can add a custom network handler implementation to your reconnect request by specifying a ReconnectSessionOptions
that uses your own network handler implementation:
await sessionManager.ReconnectAsync(sessionId,new ReconnectSessionOptions().WithNetworkHandler(customNetworkHandler));
Additional resources
- QuerySessionsAsync for information about querying available sessions.
- QuerySessionsOptions for information about the query options for the search.
- QuerySessionsResults to return a list of available sessions from your query.
- JoinSessionByIdAsync for information on joining a session by the session ID.
- JoinSessionOptions for a list of available options.
- GetJoinedSessionIdsAsync to return a list of sessions the current user is a member of.
- ReconnectToSessionAsync to reconnect to a particular session.