Information in this guide is correct from date of upload: 2023/03/07
Summary
This is a step by step authentication tutorial made to simplify the many different pages that are required to set up Google Play Games Authentication.
More information can be found here.
Requirements
- Google Play Console Developer account
- Google Play Games SDK for Unity version v11.01
- Google Play Games on a device
- Latest version of the Unity Authentication SDK with package manager
- Unity Editor version 2019.4.40f1 and above
- Unity Project building to Android
- Java JDK
- *Minify must remain unchecked when publishing
Player Settings > Publish Settings > Minify - kudos to @rbitard for reporting*
Authentication integration with Google Play Games
Initial Google Play Console setup
-
Sign in to google play console
-
Create a new app. *More detailed steps can be found on the official website here
-
Fill in your app name language and if your application is free or paid
-
Choose GAME in the app details. Your application must be type GAME to access the Setup and Management Menu
-
Select Create App
-
Go to Google Play Console > Grow > Play Games Services > Setup and management > Configuration
-
Choose the appropriate Google API selection for your game eg: No, My game doesn’t use Google APIs
-
Click on he Create button
-
Select Setup and Management>Configuration>Credentials>Configure
-
Select Google Cloud Platform
-
Next continue configuring Google Cloud Platform
Google Cloud Platform Setup
-
Choose if your OAUTH consent is External or Internal.
-
Select Create
-
Fill in the *required information
-
Select Save and continue
-
Add the following scopes: games,games_lite and drive.appdata
-
Select Update then Save and continue
-
Add test users. These should be google accounts associated with Google and Google Play Games
-
Click on Save and continue, Navigate back go the Google Play Console
Google Play Console setup continued
-
[OPTIONAL] If you previously confirmed and configured your oAuth, by following steps 1-8 in the previous section, skip ahead to step 4
-
Go to Play Games Services > Setup and management > Configuration
-
Select Configure your oAuth consent screen and confirm configuration
-
Click on Add Credential to create a credential for Android
-
Select Type Android
-
Click Create OAuth client.
-
Follow the steps proposed below
-
On Create OAuth Client ID Page - Select the Application Type Android
-
Name should match the Project Name
-
Paste your Package Name. * This can be found in the Unity Editor > Edit Project Settings > Player > Android> Other Settings > Package Name.
-
You must build your project at least once and generate a keystore before moving on to the next step. Learn more here
-
Open a Command Prompt on Windows or Terminal on a Mac
-
WINDOWS: *Required to have JDK installed
-
Generate a SHA-1 key by typing the following into your Command Prompt
-
keytool -list -keystore .android[yourkeystorename].keystore -v*
-
[If the above fails] Generate a SHA-1 by navigating to your JDK installation folder normally located under C:\Program Files\Java\jdk[version]\bin> using the Command Prompt *depending on your JDK installation
-
Copy your keystore folder path. This is normally generated when create your keystore
-
Run the following command:
-
keytool -list -keystore [keystorefolderpath][yourkeystorename].keystore -v*
-
MAC *Required to have JDK installed
-
Generate a SHA-1 key by typing the following into your Terminal
-
keytool -list -keystore ~/.android/[yourkeystorename].keystore -v*
-
[If the above fails] Generate a SHA-1 by navigating to your JDK installation folder normally located under /Library/Java/JavaVirtualMachines/jdk[VERSION] *depending on your JDK installation using the Terminal
-
Copy your keystore folder path. This is normally generated when create your keystore
-
Run the following command:
-
keytool -list -keystore [keystorefolderpath][yourkeystorename].keystore -v*
Example:
-
Copy the SHA1 key and paste it in the certificate
-
Create the credentials
-
Copy your Client ID :[yourid].apps.googleusercontent.com or download the json
-
Once complete navigate back to the Google Play Console and refresh the credentials page and select your newly created OAuth credentials and save changes
-
Return to Play Games Services > Setup and management > Configuration
-
Create a Game Server Credential by clicking Add Credentials in the Configuration page under Setup and management
-
Select Type Game Server
-
Select Create OAuth client. Make sure to follow the link proposed
-
Select Application Type Web Application
-
Enter Project Name in the Name field
-
Select Create
-
Copy your Client ID and Client Secret or download json for future reference
-
Once complete Refresh OAuth clients in the Google Play Console and select your newly created OAuth credentials and save changes
-
Go to Play Games Services > Setup and management > Configuration> Credentials and click on Get Resources. Copy the resources
-
Continue to the next section
Unity Editor Setup Google Play Games
-
Download the version v11.01 play-games-plugin-for-unity
-
Open Unity Editor
-
Make sure your build settings are set to Android
-
Import the downloaded file by selecting Assets > Import Package >Custom Package
-
***Enable the Auto Android Resolver if you do not have your own
-
Window > Google Play Games > Setup > Android Setup
-
Paste your copied xml Resources in the Resources Definition section
-
Copy the previously saved Web App Client ID and paste it in the field for the Optional Web App Client ID
-
Select Setup to save
Unity Authentication Editor Setup
-
Link your project to a corresponding dashboard project or create a new project id learn more
-
Select Edit > Project Settings > Services
-
Select Use an existing Unity project ID
-
Select corresponding Organization and dashboard project
-
To install the Authentication Package, Go to Window > Package Manager. Select Unity Registry from the packages drop down menu
-
Search for Authentication and install the package
-
To ensure the latest version of authentication click on + > Add package by name and enter com.unity.services.authentication into the field
-
In the Unity Editor menu, go to Edit > Project Settings > Services > Authentication
-
From the dropdown menu add Google Play games as an identity provider
-
Fill in Web App Client ID and Client Secret and save
Unity Script Setup
-
Create an empty game object
-
Create a script, call it GPManager and attach it to the newly created game object
-
Add the following code to load the authenticated user with google play games and Unity Authentication
using System;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using System.Threading.Tasks;
using Unity.Services.Authentication;
using Unity.Services.Core;
using UnityEngine;
public class GPManager : MonoBehaviour
{
public string Token;
public string Error;
void Awake()
{
PlayGamesPlatform.Activate();
}
async void Start()
{
await UnityServices.InitializeAsync();
await LoginGooglePlayGames();
await SignInWithGooglePlayGamesAsync(Token);
}
//Fetch the Token / Auth code
public Task LoginGooglePlayGames()
{
var tcs = new TaskCompletionSource<object>();
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
tcs.SetResult(null);
});
}
else
{
Error = "Failed to retrieve Google play games authorization code";
Debug.Log("Login Unsuccessful");
tcs.SetException(new Exception("Failed"));
}
});
return tcs.Task;
}
async Task SignInWithGooglePlayGamesAsync(string authCode)
{
try
{
await AuthenticationService.Instance.SignInWithGooglePlayGamesAsync(authCode);
Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}"); //Display the Unity Authentication PlayerID
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);
}
}
}
Testing
Check if authentication is working by using Unity Logcat or Android studio Logcat.
You must connect your device to your workstation to view the Logcat.
A successful test will print out the following:
Unity Logcat (example):
Android Studio Logcat (example):
Additionally a visual queue will be displayed upon successful login.
Conclusion
This should get you started logging in to authentication and google play games generating and linking a user id between Google and Unity.
Links for further knowledge
Authentication documentation for Unity
https://docs.unity.com/authentication/en/manual/set-up-google-play-games-signin
Disable automatic sign in
https://www.hardreset.info/devices/apps/apps-google-play-games/disable-automatically-sign-in-to-supported-games/