Google Play Games
Provide a Google Play Games sign-in option to provide a frictionless authentication experience for Android players.
Read time 2 minutesLast updated 15 hours ago
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.
Set up a Google Play Games sign-in
- 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.\
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 theSignInWithGooglePlayGamesAsync- Create a new Unity Authentication player with the Google Play Games credentials.
- Sign in an existing player using the Google Play Games credentials.
SignInWithGooglePlayGamesAsyncSignInWithGooglePlayGamesAsyncSignInWithGooglePlayGamesAsyncasync 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 theLinkWithGooglePlayGamesAsync- Sign into the cached player's account using .
SignInAnonymouslyAsync - Link the cached player's account to the Google Play Games account with .
LinkWithGooglePlayGamesAsync
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 theUnlinkGooglePlayGamesAsyncasync Task UnlinkGooglePlayGamesAsync(){ try { await AuthenticationService.Instance.UnlinkGooglePlayGamesAsync(); 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); }}