Minimum SDK version: 2.0.0
Note: Make sure you're using the correct SDK and plugin, otherwise the code samples will not work.
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. An ID token, which is an Open Authentication (ID) access token that is used to identify the player to other services such as Firebase or Google, is required.
{
((PlayGamesLocalUser)Social.localUser).GetIdToken()
}
For plugin versions running v10.14 and later, follow the Google Play Games external identity provider manual for step-by-step instructions.
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.
- Download and import the Google Play Games plugin for Unity v10.14.
- Set up your app to enable Google sign-in. Follow Google’s documentation on how to configure your game for Google sign-in.
- 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, then select Add.
- 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, then you have the option of linking the cached player to the Google Account.
- Sign into the cached player's account using SignInAnonymouslyAsync.
- Link the cached player's account to the Google account with LinkWithGoogleAsync.
For more information about cached players, read the Sign In a Cached User section.
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);
}
}