Google Play Games
Minimum SDK version: 2.1.0
This article guides you through the following scenarios in setting up authentication for players in your game with a Google Play Games account:
Note: The code example below assumes you already have the player's one-time authorization code.
- Set up a Google Play Games 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 Play Games account.
To provide a Google Play Games 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 v11.01+ to sign in the user and get the one-time authorization code.
For plugin versions running v10.14 and earlier, follow the Google ID provider article.
Set up a Google Play Games sign-in
Note: A Google Play Games sign-in is compatible with Android devices only.
Note: Google Play Games sign-in is supported by the Google Play Games plugin for Unity v11.01 and above which recommends using Play Games Services v2 SDK.
Download and import the Google Play Games Plugin for Unity.
Set up your app to enable Google sign-in. Follow Google’s documentation on how to configure your game for Google Login.
Configure your ID provider to be Google for Unity Authentication:
In the Unity Editor menu, go to Edit > Project Settings…, then select Services > Authentication from the navigation menu.
Set ID Providers to Google Play Services, then click Add.
Enter the Web App client ID in the Client ID text field.
Enter the Web App client secret in the Client Secret text field, then select Save.\
Note: You must set a Web App client ID in the plugin setup dialog to use the Authentication SDK.
Implement the Google Play Games sign-in following this sample code:
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine;
public class GooglePlayGamesExampleScript : MonoBehaviour
{
public string Token;
public string Error;
void Awake()
{
//Initialize PlayGamesPlatform
PlayGamesPlatform.Activate();
LoginGooglePlayGames();
}
public void LoginGooglePlayGames()
{
PlayGamesPlatform.Instance.Authenticate((success) =>
{
if (success == SignInStatus.Success)
{
Debug.Log("Login with Google Play games successful.");
PlayGamesPlatform.Instance.RequestServerSideAccess(true, code =>
{
Debug.Log("Authorization code: " + code);
Token = code;
// This token serves as an example to be used for SignInWithGooglePlayGames
});
}
else
{
Error = "Failed to retrieve Google play games authorization code";
Debug.Log("Login Unsuccessful");
}
});
}
}
Sign in a returning player or create new player
You can use the SignInWithGooglePlayGamesAsync
method to either:
- Create a new Unity Authentication player with the Google Play Games credentials.
- Sign in an existing player using the Google Play Games credentials.
If no Unity Authentication player in your project is associated with the credentials, SignInWithGooglePlayGamesAsync
creates a new player. If a Unity Authentication player in your project is associated with the credentials, SignInWithGooglePlayGamesAsync
signs into that player's account. This function doesn't consider the cached player, and SignInWithGooglePlayGamesAsync
replaces the cached player.
async Task SignInWithGooglePlayGamesAsync(string authCode)
{
try
{
await AuthenticationService.Instance.SignInWithGooglePlayGamesAsync(authCode);
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);
}
}
Updating a player from anonymous to a Google Play Games account
After you’ve set up anonymous authentication, if the player wants to upgrade from being anonymous to creating a Google Play Games account and sign in, your game should prompt the player to trigger the Google Play Games sign-in and get the one-time authorization code from Google. Then, call the LinkWithGooglePlayGamesAsync
API to link the player.
If a cached player exists on the SDK, you can link the cached player to the Google Play Games Account.
- Sign into the cached player's account using
SignInAnonymouslyAsync
. - Link the cached player's account to the Google Play Games account with
LinkWithGooglePlayGamesAsync
.
For more information about cached players, refer to Sign In a Cached User.
async Task LinkWithGooglePlayGamesAsync(string authCode)
{
try
{
await AuthenticationService.Instance.LinkWithGooglePlayGamesAsync(authCode);
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);
}
}
Unlink Google Play account
Use the UnlinkGooglePlayGamesAsync
API so your players can unlink their Google Play Games account. Once unlinked, if their account isn’t linked to any additional identity, it transitions to an anonymous account.
async Task UnlinkGooglePlayGamesAsync(string idToken)
{
try
{
await AuthenticationService.Instance.UnlinkGooglePlayGamesAsync(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);
}
}