UnityServiceAdapter

Internal concrete implementation of IServiceAdapter using Unity Gaming Services. Handles SDK initialization, anonymous authentication, and session management via UGS Authentication and Multiplayer packages.

Definition

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

internal class UnityServiceAdapter : IServiceAdapter

Implements: IServiceAdapter

Remarks

UnityServiceAdapter is the default (and currently only) service adapter. It is instantiated by NetworkService when Connect() is called with NetworkServiceType.UNITY.

The adapter wraps three Unity Gaming Services APIs:

  1. UnityServices (com.unity.services.core) — One-time SDK initialization via InitializeAsync().

  2. AuthenticationService (com.unity.services.authentication) — Anonymous sign-in producing a player ID and name.

  3. MultiplayerService (com.unity.services.multiplayer) — Session creation, joining, and querying with automatic relay networking.

All sessions are created with WithRelayNetwork() enabled, meaning Unity Relay handles NAT traversal transparently. After creating or joining a session, the adapter waits for the Netcode client connection (NetworkManager.WaitForClientConnection()) and player synchronization before returning.

Quick Lookup

Goal
How

Initialize UGS SDK

await adapter.Initialize()

Authenticate anonymously

await adapter.SignIn()NetworkUser

Create a relay session

await adapter.CreateSession("My Session", 4)

Join by session ID

await adapter.JoinSession(sessionId)

Query available sessions

await adapter.QuerySessions()

Sign out

await adapter.SignOut()

Properties

ServiceType

Returns NetworkServiceType.UNITY.

Constructor

Parameter
Type
Description

service

NetworkService

The parent service instance that owns this adapter

Methods

Initialize

Captures Unity's main thread context via UnityThreadUtils and initializes the UGS SDK.

Behavior:

  1. Calls UnityThreadUtils.CaptureUnityThreadInfo() to ensure async continuations run on the Unity main thread.

  2. Calls await UnityServices.InitializeAsync() to initialize the UGS SDK.

SignIn

Authenticates the local user anonymously via UGS Authentication.

Returns: A NetworkUser with the authenticated player's ID and display name (defaults to "Guest" if no name is set).

Behavior:

  1. In the Unity Editor, clears the session token first (ClearSessionToken()) to force a fresh sign-in each session.

  2. Calls AuthenticationService.Instance.SignInAnonymouslyAsync().

  3. Reads PlayerId and PlayerName from the authentication instance.

SignOut

Signs the local user out and waits for the operation to complete.

Behavior:

  • Uses a TaskCompletionSource<bool> to convert the event-based AuthenticationService.SignedOut callback into an awaitable Task.

  • Calls SignOut(true) to clear credentials.

QuerySessions

Queries UGS Multiplayer for available sessions.

Parameter
Type
Description

queryOptions

QuerySessionsOptions

Optional filter/sort criteria. Defaults to an empty options object.

Returns: A list of NetworkSessionInfo structs mapped from UGS session results.

CreateSession

Creates a new multiplayer session with Unity Relay networking enabled.

Parameter
Type
Description

sessionName

string

Display name for the session. Defaults to "Paragon Session" if null.

maxConnections

int

Maximum players allowed. Defaults to 4.

Returns: A UnityNetworkSession wrapping the created UGS session.

Behavior:

  1. Creates SessionOptions with the specified name and max players.

  2. Calls MultiplayerService.Instance.CreateSessionAsync(sessionOptions.WithRelayNetwork()).

  3. Waits for the Netcode client connection via NetworkManager.WaitForClientConnection().

  4. Creates and returns a UnityNetworkSession via UnityNetworkSession.Create(session).

JoinSession

Joins an existing session by its unique identifier.

Parameter
Type
Description

sessionID

string

The unique identifier of the session to join

Returns: A UnityNetworkSession wrapping the joined UGS session.

Behavior:

  1. Calls MultiplayerService.Instance.JoinSessionByIdAsync(sessionID).

  2. Waits for the Netcode client connection via NetworkManager.WaitForClientConnection().

  3. Creates and returns a UnityNetworkSession via UnityNetworkSession.Create(session).

Constants

max_connections

Default maximum number of players per session.

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation

See Also

Last updated