기술 자료

Multiplayer Services SDK

Multiplayer Services SDK

Leave a session

Learn how players can leave a session and what happens when they exit.
읽는 시간 1분최근 업데이트: 7일 전

When players leave a session or get kicked out of the session, their player ID is removed from the players list. If the host leaves the session, another player in the session is randomly selected as the host. The host can also remove other players from the session. The same
RemovePlayerAsync
API call is used in both cases where the host simply specifies the other player's
playerId
instead of their own. Sessions are automatically deleted when the last player in the session leaves.
The following code sample shows how to remove a player from the lobby:
C#
using Unity.Services.Multiplayer;using UnityEngine;// ...try{ // Assume 'session' is the active ISession instance the player has joined. // Leaving the session automatically removes the authenticated local player // from the backend service and safely shuts down associated network modules. await session.LeaveAsync();}catch (SessionException e){ // The Multiplayer Services SDK uses SessionException for API errors. Debug.Log(e);}

Automatic leave when the application quits

The SDK automatically calls
LeaveAsync
on every connected session when the application quits (on
Application.quitting
). You don't need to leave sessions yourself during shutdown.
If the application exits without raising
Application.quitting
, for example because it crashes or the process is killed, the player isn't removed right away. The player stays a member of the session until the service removes them, and they can reconnect to the session when they restart the game.

Leave a session as a server

For information on server-specific outcomes, refer to Session hosts, clients, and servers.