Prepare your game for Unity Gaming Services
Initialize Unity Gaming Services in your project, and authenticate the player to prepare for multiplayer functionality.
Read time 1 minuteLast updated 12 hours ago
You must first initialize Unity Gaming Services for your project before you can use its features and begin your multiplayer game.
Initialize UGS and authenticate the player
To initialize Unity Gaming Services, and authenticate the player:- In the Unity Editor, go to the Project window, then select Assets > Scripts.
- Right-click (macOS: Ctrl+click) Scripts.
- Select Create > Scripting > MonoBehaviour Script.
-
Name the script .
SessionManager - Right-click (macOS: Ctrl+click) in the Hierarchy window and select Create Empty.
-
Enter as the name.
SessionManager -
Select this new GameObject.
SessionManager - In its Inspector window, select Add Component.
-
Select your script.
SessionManager -
In the Project window, double-click the script. The script opens in your preselected IDE.
SessionManager -
Replace the contents of with the following code:
SessionManager.cs
In the above code:using UnityEngine;using Unity.Services.Core;using System;using Unity.Services.Authentication;public class SessionManager : MonoBehaviour{ async void Start() { try { await UnityServices.InitializeAsync(); await AuthenticationService.Instance.SignInAnonymouslyAsync(); Debug.Log($"Sign in anonymously succeeded! PlayerID: {AuthenticationService.Instance.PlayerId}"); } catch (Exception e) { Debug.LogException(e); } }}- The line initializes Unity Gaming Services SDKs.
await UnityServices.InitializeAsync(); - The line anonymously authenticates the player.
await AuthenticationService.Instance.SignInAnonymouslyAsync();
- The line