docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Interface ISession

    An interface that can be used by a client to access the information of a session.

    Namespace: Unity.Services.Multiplayer
    Assembly: Unity.Services.Multiplayer.dll
    Syntax
    public interface ISession

    Properties

    AvailableSlots

    Available slots in the session

    Declaration
    int AvailableSlots { get; }
    Property Value
    Type Description
    int

    Code

    The join code of the session.

    Declaration
    string Code { get; }
    Property Value
    Type Description
    string
    Remarks

    Prefer this to ID when player-visible strings are needed. Unlike IDs, join codes are much shorter to type and avoid visually ambiguous characters.

    ConcurrencyControlEnabled

    Applies concurrency control by adding an If-match header with the latest version of the session to applicable session operations. When enabled, operations are only allowed if the latest version matches the current version on the server. Applies to the following operations:

    • deleting a session
    • removing a player from a session
    • updating a session player
    • updating a session information
    Declaration
    bool ConcurrencyControlEnabled { get; set; }
    Property Value
    Type Description
    bool

    CurrentPlayer

    The current player in the session.

    Declaration
    IPlayer CurrentPlayer { get; }
    Property Value
    Type Description
    IPlayer

    HasPassword

    True if the session has a password, false otherwise

    Declaration
    bool HasPassword { get; }
    Property Value
    Type Description
    bool

    Host

    PlayerID of the session host.

    Declaration
    string Host { get; }
    Property Value
    Type Description
    string

    Id

    The ID of the session.

    Declaration
    string Id { get; }
    Property Value
    Type Description
    string
    Remarks

    Unique within a project and environment. Sessions can be joined by Id.

    See Also
    JoinByIdAsync(string, JoinSessionOptions)

    IsHost

    Whether the current player is the host of the session.

    Declaration
    bool IsHost { get; }
    Property Value
    Type Description
    bool

    IsLocked

    Whether the session is locked.

    Declaration
    bool IsLocked { get; }
    Property Value
    Type Description
    bool
    Remarks

    Locked sessions cannot be joined by anyone.

    See Also
    IsLocked

    IsMember

    Returns whether the player is a member of the session

    Declaration
    bool IsMember { get; }
    Property Value
    Type Description
    bool

    IsPrivate

    Whether the session is private.

    Declaration
    bool IsPrivate { get; }
    Property Value
    Type Description
    bool
    Remarks

    Private sessions or not visible in queries and cannot be joined with quick-join. They can still be joined by ID or by Code.

    See Also
    IsPrivate

    IsServer

    Whether the local owner of the session handle is a server managing the session.

    Declaration
    bool IsServer { get; }
    Property Value
    Type Description
    bool

    MaxPlayers

    The total number of players allowed in the session, including the host.

    Declaration
    int MaxPlayers { get; }
    Property Value
    Type Description
    int
    See Also
    MaxPlayers

    Name

    The Name of the session.

    Declaration
    string Name { get; }
    Property Value
    Type Description
    string
    Remarks

    Does not have to be unique. Sessions can be filtered by name in a query.

    See Also
    Name
    QuerySessionsOptions

    Network

    Retrieve the client network handle

    Declaration
    IClientSessionNetwork Network { get; }
    Property Value
    Type Description
    IClientSessionNetwork

    PlayerCount

    The player count of the session

    Declaration
    int PlayerCount { get; }
    Property Value
    Type Description
    int

    Players

    The list of players in the session

    Declaration
    IReadOnlyList<IReadOnlyPlayer> Players { get; }
    Property Value
    Type Description
    IReadOnlyList<IReadOnlyPlayer>

    Properties

    The properties of the session.

    Declaration
    IReadOnlyDictionary<string, SessionProperty> Properties { get; }
    Property Value
    Type Description
    IReadOnlyDictionary<string, SessionProperty>
    Remarks

    You can modify session properties via an IHostSession.
    To obtain an IHostSession from an ISession call AsHost().

    See Also
    AsHost()
    IHostSession

    State

    The current state of the session.

    Declaration
    SessionState State { get; }
    Property Value
    Type Description
    SessionState

    Type

    The type is a client-side key used to uniquely identify a session.

    Declaration
    string Type { get; }
    Property Value
    Type Description
    string

    Methods

    AsHost()

    Returns a read-write handle to the session with host privileges.

    Declaration
    IHostSession AsHost()
    Returns
    Type Description
    IHostSession

    A session with higher privileges.

    Exceptions
    Type Condition
    SessionException

    Throws a Forbidden error when the caller is not the host.

    GetPlayer(string)

    Retrieves information about a session member player by their playerId.

    Declaration
    IReadOnlyPlayer GetPlayer(string playerId)
    Parameters
    Type Name Description
    string playerId

    The ID of the player to retrieve.

    Returns
    Type Description
    IReadOnlyPlayer

    The IReadOnlyPlayer instance if found; otherwise, null.

    See Also
    GetPlayer(string)

    HasPlayer(string)

    Checks if a player with the given playerId is in the session.

    Declaration
    bool HasPlayer(string playerId)
    Parameters
    Type Name Description
    string playerId

    The player ID to check for session membership.

    Returns
    Type Description
    bool

    true if the player is a member of the session; otherwise, false.

    LeaveAsync()

    Leaves the session.

    Declaration
    Task LeaveAsync()
    Returns
    Type Description
    Task

    A task for the operation.

    Remarks

    This method will be called automatically when the application is quitting.

    Exceptions
    Type Condition
    SessionException

    Thrown when called by a non-session-member. The Error property will be set to InvalidOperation.

    ReconnectAsync()

    Reconnects to the session.

    Declaration
    Task ReconnectAsync()
    Returns
    Type Description
    Task

    A task for the operation.

    Remarks

    Reconnecting is necessary when a player has momentarily disconnected from the network but didn't gracefully leave the session.

    Exceptions
    Type Condition
    SessionException

    Throws a SessionDeleted error when the session is already deleted.

    RefreshAsync()

    Refreshes the session data.

    Declaration
    Task RefreshAsync()
    Returns
    Type Description
    Task

    A task for the operation.

    Exceptions
    Type Condition
    SessionException

    Throws a SessionDeleted error when the session is already deleted.

    SaveCurrentPlayerDataAsync()

    Save changes to the current player.

    Declaration
    Task SaveCurrentPlayerDataAsync()
    Returns
    Type Description
    Task

    A Task representing the asynchronous operation.

    Events

    Changed

    Event that is invoked when the session changes.

    Declaration
    event Action Changed
    Event Type
    Type Description
    Action
    Remarks

    This includes any type of changes to the session (properties, players, etc.).

    Deleted

    Event that is invoked when the session is deleted.

    Declaration
    event Action Deleted
    Event Type
    Type Description
    Action

    PlayerHasLeft

    Event that is invoked when a player has left the session.

    Declaration
    event Action<string> PlayerHasLeft
    Event Type
    Type Description
    Action<string>
    Remarks

    The string parameter represents the ID of the player who has left. This event is called right after the session gets updated.

    PlayerJoined

    Event that is invoked when a player has joined the session.

    Declaration
    event Action<string> PlayerJoined
    Event Type
    Type Description
    Action<string>
    Remarks

    The string parameter represents the ID of the player who has joined. This event is called right after the session gets updated.

    PlayerLeaving

    Event that is invoked when a player is leaving the session.

    Declaration
    event Action<string> PlayerLeaving
    Event Type
    Type Description
    Action<string>
    Remarks

    The string parameter represents the ID of the player who is leaving. This event is called right before the session gets updated.

    PlayerLeft

    Event that is invoked when a player is leaving the session.

    Declaration
    [Obsolete("PlayerLeft has been deprecated. Use PlayerLeaving instead (UnityUpgradable) -> PlayerLeaving")]
    event Action<string> PlayerLeft
    Event Type
    Type Description
    Action<string>
    Remarks

    The string parameter represents the ID of the player who is leaving. This event is called right before the session gets updated.

    PlayerPropertiesChanged

    Event that is invoked when player properties are changed.

    Declaration
    event Action PlayerPropertiesChanged
    Event Type
    Type Description
    Action
    Remarks

    If the properties are already up to date locally, this event will not trigger. Example: This will not trigger for the host after it updates the properties, as the changes are already reflected in the host's local session.

    RemovedFromSession

    Event that is invoked when the current player is removed from the session.

    Declaration
    event Action RemovedFromSession
    Event Type
    Type Description
    Action

    SessionHostChanged

    Event that is invoked when the session host changed.

    Declaration
    event Action<string> SessionHostChanged
    Event Type
    Type Description
    Action<string>
    Remarks

    The string parameter represents the ID of the new session host.

    SessionMigrated

    Event that is invoked when the session has finished migrating hosts.

    Declaration
    event Action SessionMigrated
    Event Type
    Type Description
    Action

    SessionPropertiesChanged

    Event that is invoked when session properties are changed.

    Declaration
    event Action SessionPropertiesChanged
    Event Type
    Type Description
    Action
    Remarks

    If the properties are already up to date locally, this event will not trigger. Example: This will not trigger for the host after it updates the properties, as the changes are already reflected in the host's local session.

    StateChanged

    Event that is invoked when the session state changes.

    Declaration
    event Action<SessionState> StateChanged
    Event Type
    Type Description
    Action<SessionState>
    Remarks

    The SessionState parameter represents the new state of the session.

    Extension Methods

    MatchmakerExtensions.GetMatchmakingResults(ISession)
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)