Session states
Reference the lifecycle states a session can be in while it's created, connected, and removed.
阅读时间3 分钟最后更新于 1 天前
The enum represents the current lifecycle state of a multiplayer session. Use the session's property or subscribe to to react when a session transitions between states.
SessionStateStateStateChangedA session can be in one of the following states:
State | Description | When to use |
|---|---|---|
| The session has no active lifecycle state, either because it isn't initialized yet or because the player's authentication was cleared. | Before a session is fully initialized, or after the player signs out. |
| The session is active and the client is connected to the session. | During normal gameplay or while a session is available for players to participate in. |
| The client was connected to the session but has left it or been removed from it. | After the player leaves or is removed, or while you handle reconnection. |
| The session has been removed or deleted and is no longer valid for use. | After the session has been closed or cleaned up. |
What session state tracks
A session's state describes the local client's connection to the session. It doesn't describe the session's data or its player list:
- A session keeps its data and properties while a client is .
Disconnected - A player who loses their network connection without leaving is still a member of the session until the host or the service removes them. While they're still a member, they can reconnect.
- To track other players joining and leaving, use session player events instead of the session state.
How a session moves between states
A session typically moves through the following lifecycle:
The following events change a session's state:
Transition | Cause |
|---|---|
To | The client creates, joins, or reconnects to the session. |
To | The client calls |
To | The host or server calls |
To | The player's authentication access token is cleared, for example when the player signs out. |
When to use this state information
Check the current state when your game logic depends on whether the session is usable, reconnecting, or already invalid. For example, you might defer gameplay setup until a session reaches , show a "returned to menu" screen or a rejoin flow when a session becomes , and stop all operations when the state becomes .
ConnectedDisconnectedDeletedThe following example listens for state changes and logs the current lifecycle state:
void OnSessionStateChanged(SessionState newState) { Debug.Log($"Session state changed: {newState}");}session.StateChanged += OnSessionStateChanged;