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 : NetworkSessionInherits: 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
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.
unitySession
ISession
The Unity Gaming Services session to wrap
Returns: A fully-synchronized UnityNetworkSession instance.
Behavior:
Creates a new
UnityNetworkSessionvia the private constructor.Awaits
Yield.WaitUntil(() => session.synchronized)— for session hosts, this is immediatelytrue; for joining clients, it waits until theSynchronizationMessageis received from the host.
Constructor (private)
Passes session metadata to the base NetworkSession constructor:
unitySession.Id— unique session identifierunitySession.Name— display nameunitySession.MaxPlayers— connection limitunitySession.Host— host player IDGetSessionData(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.
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
Synchronization wait on join
When a client joins a session, Create() blocks until the host sends a SynchronizationMessage containing the current player list. If the host is unreachable or the message is lost, the await will hang indefinitely. Implement a timeout at the calling layer if needed.
KickPlayer requires host privileges
KickPlayer() calls AsHost() on the UGS session, which throws if the caller is not the session host. Always check session.IsOwner before calling.
Private constructor
The constructor is private — always use UnityNetworkSession.Create(session) to instantiate. This ensures synchronization completes before the session object is used.
See Also
NetworkSession — abstract base class
UnityServiceAdapter — adapter that creates this session
NetworkPlayer — player record used by
KickPlayer()Unity Adapters — subsystem overview
Last updated