Get started

Before you begin using Authentication, you'll need to do the following:

  1. Sign up for for UGS.
  2. Link your dashboard to a Unity Editor project.
  3. Install the SDK in your game code.
  4. Initialize the SDK.

Sign up

To use Authentication, you need to sign up to Unity Gaming Services, as it’s part of UGS.

If you don't have a Unity account, [create one]https://docs.unity.com/ugs/manual/overview/manual/unity-gaming-services-home) and create a new project to sign up for Unity Gaming Services.

Important: Only Organization Owners can sign up for Authentication.

  1. Sign into the Unity Services Dashboard.
  2. Select Explore Services in the side panel.
  3. Select Sign Up in the top banner and follow the instructions.

To use the Unity Authentication service, you’re required to link your project to a cloud project in the Unity Editor through a project ID. Follow these steps to get your project ID.

  1. In the Unity Editor menu at the top of the page, select EditProject Settings > Services tab.
  2. If you’re not already signed in with your Unity ID, either create a new Unity ID or sign in.
  3. If you want to create a new project, select your organization and click Create. If you want to link to an existing project, click I already have a Unity Project ID. Then, select your organization and project from the dropdown, and click Link.

Now you can find your project ID from the Settings tab in the Services window.

Install the SDK

To install the latest Authentication package for Unity:

  1. In the Unity Editor, open Window > Package Manager.
  2. In the Package Manager, select the Unity Registry list view.
  3. Search for Authentication, or locate it in the package list.
  4. Select the package, then select Install.
  5. (Optional) To install Unity Player Accounts:
    • Add the com.unity.services.playeraccounts package:
      • In the Package Manager, select the plus icon with the down arrow next to it.
      • For Unity Editor version 2021 or later, select Add package by name..., paste com.unity.services.playeraccounts, then select Add.
      • For Unity Editor version 2020.3 or later, select Add package from git URL, paste com.unity.services.playeraccounts, then select Add.

See the Package Manager documentation for more information.

Initialize the Unity Services SDK

To implement Unity Authentication in your game, after installing the Authentication SDK, initialize all the Unity Services SDKs included in the project following this code snippet:

using Unity.Services.Core;
using UnityEngine;

public class InitializationExample : MonoBehaviour
{
	async void Awake()
	{
		try
		{
			await UnityServices.InitializeAsync();
		}
		catch (Exception e)
		{
			Debug.LogException(e);
		}
	}
}

Register authentication events

To receive updates about the status of your player, register a function to the SignedIn, SignInFailed and SignedOut event handlers.

// Setup authentication event handlers if desired
void SetupEvents() {
  AuthenticationService.Instance.SignedIn += () => {
    // Shows how to get a playerID
    Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");

    // Shows how to get an access token
    Debug.Log($"Access Token: {AuthenticationService.Instance.AccessToken}");

  };

  AuthenticationService.Instance.SignInFailed += (err) => {
    Debug.LogError(err);
  };

  AuthenticationService.Instance.SignedOut += () => {
    Debug.Log("Player signed out.");
  };
 
  AuthenticationService.Instance.Expired += () =>
    {
        Debug.Log("Player session could not be refreshed and expired.");
    };
}