Deprecated: The approach documented below relies on the Google Sign-In API and games v1 SDK. Google has deprecated them for new titles as of Feburary 2025. Please see the notice in Google's documentation for more details.
Unity will be evaluating how best to support Sign in with Google going forward. In the mean time we recommend using Google Play Games for a frictionless Android login experience, or Unity Player Accounts which supports a web-based Sign in with Google flow on all platforms.
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.
Attention: The following concerns products or services (each a “Third Party Product”) that are not developed, owned, or operated by Unity. This information might not be up-to-date or complete, and is provided to you for your information and convenience only. Your access and use of any Third Party Product is governed solely by the terms and conditions of such Third Party Product. Unity makes no express or implied representations or warranties regarding such Third Party Products, and will not be responsible or liable, directly or indirectly, for any actual or alleged damage or loss arising from your use thereof (including damage or loss arising from any content, advertising, products or other materials on or available from the provider of any Third Party Products).
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.
- 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, you can link 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, refer to Sign In a Cached Player.
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);
}
}
Unlink Google account
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()
{
try
{
await AuthenticationService.Instance.UnlinkGoogleAsync();
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);
}
}