UnityNetworkSession

Concrete NetworkSession implementation that wraps Unity Gaming Services' ISession. Provides leave, kick, and refresh operations by delegating to the underlying UGS session API.

Definition

Namespace: Paragon.Core.Network Assembly: Paragon.dll

public class UnityNetworkSession : NetworkSession

Inherits: NetworkSession

Remarks

UnityNetworkSession is created by UnityServiceAdapter after a session is created or joined via UGS Multiplayer. It uses a static async factory method (Create) rather than a public constructor, because the creation process must wait for player synchronization before the session is ready for use.

The class extracts session metadata (ID, name, max players, host, properties) from Unity's ISession and passes it to the base NetworkSession constructor. The base class handles player tracking, connection approval, and message-based synchronization.

Session properties from ISession.Properties are flattened into a Dictionary<string, string> via GetSessionData(), discarding the UGS SessionProperty wrapper.

Quick Lookup

Goal
How

Create a session wrapper

await UnityNetworkSession.Create(unitySession)

Leave the session

await session.Leave()

Kick a player (host only)

await session.KickPlayer(player)

Refresh session data from UGS

await session.RefreshAsync()

Check players

session.Players, session.HasPlayer(id) (inherited)

Methods

Create

Static async factory method that creates a UnityNetworkSession and waits for player synchronization before returning.

Parameter
Type
Description

unitySession

ISession

The Unity Gaming Services session to wrap

Returns: A fully-synchronized UnityNetworkSession instance.

Behavior:

  1. Creates a new UnityNetworkSession via the private constructor.

  2. Awaits Yield.WaitUntil(() => session.synchronized) — for session hosts, this is immediately true; for joining clients, it waits until the SynchronizationMessage is received from the host.

Constructor (private)

Passes session metadata to the base NetworkSession constructor:

  • unitySession.Id — unique session identifier

  • unitySession.Name — display name

  • unitySession.MaxPlayers — connection limit

  • unitySession.Host — host player ID

  • GetSessionData(unitySession) — flattened session properties

Leave

Leaves the current session by calling the UGS session API.

Behavior: Delegates to unitySession.LeaveAsync().

KickPlayer

Removes a player from the session. Only callable by the session host.

Parameter
Type
Description

player

NetworkPlayer

The player to remove from the session

Behavior: Calls unitySession.AsHost().RemovePlayerAsync(player.UserID). Will throw if the caller is not the session host.

RefreshAsync

Refreshes the session data from the UGS backend.

Behavior: Delegates to unitySession.RefreshAsync() to pull the latest session state from the server.

GetSessionData (private static)

Converts UGS session properties into a flat dictionary.

Returns: A Dictionary<string, string> mapping property keys to their string values, extracted from ISession.Properties.

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation

See Also

Last updated