Unity Player Accounts

Overview

Unity Player Accounts is Unity's comprehensive sign-in solution that supports persistence across games, devices and platforms. It's an end-to-end account system that includes a user flow UX, email and password, social sign-in options, and data access and deletion flows.

Unity Player Accounts is an external identity provider which is natively integrated with Unity Gaming Services and the Authentication SDK. It has multiple benefits such as SSO, organization level player pool, and cross-game, cross-device or cross-platform gameplay.

Technical FeatureDescription
Integrated with Unity Gaming Services and EditorPlayer Accounts is the out-of-the-box sign-in and identity solution for Unity Gaming Services and Unity's ecosystem. It's seamlessly integrated with Authentication and offers additional features such as organization level player identifiers.
Cross-platform and cross-deviceYour players can sign up and sign into your games across a range of platforms, including iOS, Android, Windows, and Mac.
Cross-gameLeverage organization-level player identifiers to maintain persistent player profiles across all of your games.
Streamlined user sign-up & sign-in JourneyYour players have the flexibility to sign up or sign in to their accounts using their preferred method, such as email and password or social accounts like Google or Apple.
Integrate once and forget itOur browser-based sign up and sign-in flow is automatically updated with new features without any integration or update for your game. Focus on building and running your game, without the need to reintegrate your authentication solution. Once integrated, you can rely on Player Accounts to stay up to date with the latest security improvements and features.

Get started

To get started you must first install the Authentication package, and then configure a Unity Player Accounts ID provider in the Unity Cloud Dashboard.

The C# methods required for interacting with Unity Player Accounts are included within the Authentication package in the dedicated PlayerAccountService namespace.

Set up the Unity Player Accounts ID provider

You can set up Unity Player Accounts directly in the Unity Cloud Dashboard.

  1. In the Unity Editor select Services > Authentication > Configure from the navigation menu.
  2. Select Go to Dashboard to open the Authentication Identity providers web page.
  3. On the web page, select Add Identity Provider and select Unity Player Accounts.
  4. Set up the provider and select Add provider in the pop-up window.
  5. Return to Services > Authentication > Configure in the Unity Editor and select Refresh. This displays the Unity Player Accounts ID provider.
  6. In the Unity Editor, navigate to Services > Unity Player Accounts > Configure and ensure the Client ID is correctly set. This is also where you can optionally configure scopes and custom redirect URI schemes.

Note: If you want the sign in flow to work in the Unity Editor play mode, include the PC platform when adding the provider.

Note: The authentication flow works without the need for configuring the redirect URI, provided that the registered URIs follow the prescribed format:

  • Desktop/Standalone: http://localhost/callback
  • Android/IOS default deep link URI: unitydl://com.unityplayeraccounts.{unity-project-id}

Before you publish your game

Review Unity's compliance and branding guidelines:

  • You must provide a way for players to request data deletion and disclosure requests for your application and their Unity Player Account.
  • Include a link to the Unity Player Accounts account deletion page: https://player-account.unity.com/delete-account
  • Include a link to the Unity Player Accounts privacy support email for data disclosure or other privacy requests: unity-player-login-privacy@unity3d.com
  • You must always ensure compliance with Unity Terms of Service.
  • You must always maintain, display, and abide by a conspicuously placed Privacy Policy that makes appropriate disclosure to end-users, complies with all applicable privacy and data protection laws and regulations, and grants Unity the necessary rights required for the use of the Unity Player Accounts service.

Unity Player Accounts sample

The Authentication package includes a sample scene which is fully integrated with Unity Player Accounts. This will allow you to try out the flow after completing the confiugration steps above.

  • Select Window > Package Manager to import the sample. Look for Authentication and select Import under Samples.

  • Open the sample scene under Assets > Samples > Authentication > [Package-Version] > UI Example > UnityPlayerAccountsUIExample.

Sign in a returning player or create new player

Use the PlayerAccountService.Instance.StartSignInAsync method to start the player sign-in flow for Unity Player Accounts. This will open the system browser to https://player-login.unity.com, prompt the player to sign in, and then return them to the application with their Unity Player Accounts credentials.

After they have signed in to Unity Player Accounts and returned to your application with their credentials, use the AuthenticationService.Instance.SignInWithUnityAsync method to either:

  • Create a new Unity Authentication player with the Unity Player Accounts credentials.
  • Sign in an existing player using the Unity Player Accounts credentials.

The PlayerAccountService.Instance.SignedIn event will let us know when the browser-based Unity Player Accounts sign in has succeeded and we can proceed with signing in to Unity Authentication.

The credentials required to sign in to Unity Authentication are the access token available on PlayerAccountService.Instance.AccessToken.

async void Start()
{
await UnityServices.InitializeAsync();
// Register the Unity Player Accounts sign-in event handler after services initialization.
PlayerAccountService.Instance.SignedIn += SignInWithUnityAuth;
}
// Call this from a button or other application-specific trigger to begin the sign-in flow.
async void StartPlayerAccountsSignInAsync()
{
if (PlayerAccountService.Instance.IsSignedIn)
{
// If the player is already signed into Unity Player Accounts, proceed directly to the Unity Authentication sign-in.
SignInWithUnityAuth();
return;
}
try
{
// This will open the system browser and prompt the user to sign in to Unity Player Accounts
await PlayerAccountService.Instance.StartSignInAsync();
}
catch (PlayerAccountsException ex) {
// Compare error code to PlayerAccountsErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
}
async Task SignInWithUnityAuth()
{
try
{
await AuthenticationService.Instance.SignInWithUnityAsync(PlayerAccountService.Instance.AccessToken);
Debug.Log("SignIn is successful.");
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
}
async void Start()
{
    await UnityServices.InitializeAsync();

    // Register the Unity Player Accounts sign-in event handler after services initialization.
    PlayerAccountService.Instance.SignedIn += SignInWithUnityAuth;
}

// Call this from a button or other application-specific trigger to begin the sign-in flow.
async void StartPlayerAccountsSignInAsync()
{
    if (PlayerAccountService.Instance.IsSignedIn)
    {
        // If the player is already signed into Unity Player Accounts, proceed directly to the Unity Authentication sign-in.
        SignInWithUnityAuth();
        return;
    }

    try
    {
        // This will open the system browser and prompt the user to sign in to Unity Player Accounts
        await PlayerAccountService.Instance.StartSignInAsync();
    }
    catch (PlayerAccountsException ex) {
        // Compare error code to PlayerAccountsErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
}

 async Task SignInWithUnityAuth()
{
    try
    {
        await AuthenticationService.Instance.SignInWithUnityAsync(PlayerAccountService.Instance.AccessToken);
        Debug.Log("SignIn is successful.");
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
}

If you have an existing player who is signed in already, either anonymous or using a different external ID provider, you can allow them to link with Unity Player Accounts. Your game should prompt the player to trigger the Unity Player Accounts sign-in flow as outlined above. Then, call the LinkWithUnityAsync API to link the player using the Unity Player Accounts access token.

For linking to be successful, the player needs to be signed in to Unity Authentication. Please refer to the page on Unity Authentication sessions for more details on the Unity Authentication session lifecycle.

async Task LinkWithUnityAsync(string accessToken)
{
try
{
await AuthenticationService.Instance.LinkWithUnityAsync(accessToken);
Debug.Log("Link is successful.");
}
catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked)
{
// Prompt the player with an error message.
Debug.LogError("This user is already linked with another account. Log in instead.");
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
}
async Task LinkWithUnityAsync(string accessToken)
        {
            try
            {
                await AuthenticationService.Instance.LinkWithUnityAsync(accessToken);
                Debug.Log("Link is successful.");
            }
            catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked)
            {
                // Prompt the player with an error message.
                Debug.LogError("This user is already linked with another account. Log in instead.");
            }
            catch (AuthenticationException ex)
            {
                // Compare error code to AuthenticationErrorCodes
                // Notify the player with the proper error message
                Debug.LogException(ex);
            }
            catch (RequestFailedException ex)
            {
                // Compare error code to CommonErrorCodes
                // Notify the player with the proper error message
                Debug.LogException(ex);
            }
        }

Use the AuthenticationService.Instance.UnlinkUnityAsync API so your players can unlink their Unity Player account. Once unlinked, if their account is not linked to any additional identity, it transitions to an anonymous account.

async Task UnlinkUnityAsync()
{
try
{
await AuthenticationService.Instance.UnlinkUnityAsync();
Debug.Log("Unlink is successful.");
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
}
async Task UnlinkUnityAsync()
{
   try
   {
       await AuthenticationService.Instance.UnlinkUnityAsync();
       Debug.Log("Unlink is successful.");
   }
   catch (AuthenticationException ex)
   {
       // Compare error code to AuthenticationErrorCodes
       // Notify the player with the proper error message
       Debug.LogException(ex);
   }
   catch (RequestFailedException ex)
   {
       // Compare error code to CommonErrorCodes
       // Notify the player with the proper error message
       Debug.LogException(ex);
   }
}

Sign out from Unity Player Accounts

The PlayerAccountService.Instance tracks if the player has signed in to their Unity Player Account. This is separate from tracking if the player is signed in to Unity Authentication, as done by AuthenticationService.Instance. This means that you have individual control over signing out from Unity Authentication and Unity Player Accounts.

Use PlayerAccountService.Instance.SignOut to sign the player out of Unity Player Accounts on the device.

Because the PlayerAccountService.Instance does not persist a session token on the device, there is no option to clear the session token.

public void SignOut(bool clearSessionToken = false)
{
// Sign out of Unity Authentication, with the option to clear the session token
AuthenticationService.Instance.SignOut(clearSessionToken);
// Sign out of Unity Player Accounts
PlayerAccountService.Instance.SignOut();
}
public void SignOut(bool clearSessionToken = false)
{
    // Sign out of Unity Authentication, with the option to clear the session token
    AuthenticationService.Instance.SignOut(clearSessionToken);

    // Sign out of Unity Player Accounts
    PlayerAccountService.Instance.SignOut();
}