Initialize UGS and authenticate the player
Initialize Unity Gaming Services in your project, and authenticate the player to prepare for multiplayer functionality.
읽는 시간 1분최근 업데이트: 2일 전
You must first initialize Unity Gaming Services for your project before you can use its features and begin your multiplayer game. 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