Google

Minimum SDK version: 2.0.0

Note: For the code sameples below to work, you must use the correct SDK and plugin versions.

This article guides you through the following scenarios in setting up authentication for players in your game with a Google account:

  • Set up a Google sign-in.
  • Sign in a returning user or create new user.
  • Updating a user from an anonymous sign-in to a platform sign-in via a Google account.

To provide a Google sign-in option for the players in your game, create your app in the Google Play Console and install Google Play Games plugin for Unity v10.14 to sign in the player and get the access token. This requires an Open Authentication (ID) access token to identify the player to other services such as Firebase or Google.

{
    ((PlayGamesLocalUser)Social.localUser).GetIdToken()
}

For plugin versions running v10.14 and later, refer to Google Play Games.

Note: The code example below assumes you already have the player's Google ID Token.

Set up a Google sign-in

Note: Google sign-in is supported by Google Play Games plugin for Unity v10.14 and below which uses Play Games Services v1 SDK.

  1. Download and import the Google Play Games plugin for Unity v10.14.
  2. Set up your app to enable Google sign-in. Follow Google’s documentation on how to configure your game for Google sign-in.
  3. Configure your ID provider to be Google for Unity Authentication:
    1. In the Unity Editor menu, go to Edit > Project Settings…, then select Services > Authentication from the navigation menu.
    2. Set ID Providers to Google, then select Add.
    3. Enter the Web App client ID in the Client ID text field, then select Save.

Note: You must set a Web App client ID in the plugin setup dialog to use the Authentication SDK.

void InitializePlayGamesLogin()
{
    var config = new PlayGamesClientConfiguration.Builder()
        // Requests an ID token be generated.  
        // This OAuth token can be used to
        // identify the player to other services such as Firebase.
        .RequestIdToken()
        .Build();

    PlayGamesPlatform.InitializeInstance(config);
    PlayGamesPlatform.DebugLogEnabled = true;
    PlayGamesPlatform.Activate();
}

void LoginGoogle()
{
    Social.localUser.Authenticate(OnGoogleLogin);
}

void OnGoogleLogin(bool success)
{
    if (success)
    {
        // Call Unity Authentication SDK to sign in or link with Google.
        Debug.Log("Login with Google done. IdToken: " + ((PlayGamesLocalUser)Social.localUser).GetIdToken());
    }
    else
    {
        Debug.Log("Unsuccessful login");
    }
}

Sign in a returning player or create new player

Use the SignInWithGoogleAsync method to either:

  • Create a new Unity Authentication player with the Google credentials.
  • Sign in an existing player using the Google credentials.
async Task SignInWithGoogleAsync(string idToken)
{
    try
    {
        await AuthenticationService.Instance.SignInWithGoogleAsync(idToken);
        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);
    }
}

Update a player from anonymous to a Google account

After you’ve set up anonymous authentication, if the player wants to upgrade from being anonymous to creating a Google account and sign in, your game should prompt the player to trigger the Google sign-in and get the ID token from Google. Then, call the LinkWithGoogleAsync API to link the player to the Google ID token.

If a cached player exists on the SDK, you can link the cached player to the Google Account.

  1. Sign into the cached player's account using SignInAnonymouslyAsync.
  2. Link the cached player's account to the Google account with LinkWithGoogleAsync.

For more information about cached players, refer to Sign In a Cached User.

async Task LinkWithGoogleAsync(string idToken)
{
    try
    {
        await AuthenticationService.Instance.LinkWithGoogleAsync(idToken);
        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 UnlinkGoogleAsync API so your players can unlink their Google account. Once unlinked, if their account isn’t linked to any additional identity, it transitions to an anonymous account.

async Task UnlinkGoogleAsync(string idToken)
{
   try
   {
       await AuthenticationService.Instance.UnlinkGoogleAsync(idToken);
       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);
   }
}