Appearance
Appearance
Google Play Games allows Android users to sign in with their Google Play Games player profile for authentication purposes. Google Play Games comes with two versions, both using different authentication approaches. These API versions, called hereinafter Legacy API and API v2, are both supported by Metaplay but your game should only implement one of them. The Google Play Console provides metadata configuration and managing services for your player base on android devices. It also supports achievements and adding friends, which can foster community and help create a better player experience.
The Google Play Console provides metadata configuration and managing services for your player base on android devices. It also supports achievements and adding friends, which can foster community and help create a better player experience. The first thing you need to do to enable integration is to create a game project in the Google Play Console and set up all of the authentications for your game.
Detailed instructions on how to set your project up can be found in the official Google Play Games documentation.
Create both an Android credential and a Game server credential. Download the Game server credential JSON from Google Cloud Console (where it is called either a Web Client or Web Application) and save it locally as Backend/Secrets/google_play_oauth_client_creds.json
. Note that despite the name, this is a Game Server credential and should never be included in client builds.
Finally, in Google Play Console, copy the Application ID from the Configuration
page where it's listed as Project ID
. Note that this is not the Project ID in Google Cloud Console.
The game server needs to be configured to accept and validate Google Play Games login requests. To do that, you need to modify the appropriate Options
file, located in /Backend/Server/Config/Options.<env>.yaml
, where <env>
is the desired environment. If you need to freshen up on Runtime Options, check out Working with Runtime Options.
GooglePlayStore:
EnableGoogleAuthentication: true
GooglePlayOAuth2ClientCredentialsPath: "./Secrets/google_play_oauth_client_creds.json"
GooglePlayApplicationId: [Project ID from Google Play Console]
Storing Secrets Securely
To avoid storing secrets within the code repository, GooglePlayOAuth2ClientCredentialsPath
may also be defined as a path to AWS Secrets Manager. For example, aws-sm://us-east-1#google_play_oauth_client_creds
would denote credentials in Secrets Manager on us-east-1
with name google_play_oauth_client_creds
.
Clone or download the Play Games Plugin for Unity and import the current-build/GooglePlayGamesPluginForUnity-X.YY.ZZ.unitypackage
file through the toolbar using Assets > Import Package > Custom Package. After that, you'll want to make sure that the build platform is set to Android. Do that by selecting File > Build Settings and checking that 'Android' is selected, then click on 'Switch Platform'. There should be a new 'Google Play Games' menu added under the Window tab. If this doesn't work click on Assets > Refresh.
Platform Dependency
Installing the Google Play Games plugin will also install the External Dependency Manager for Unity (EDM4U). For more information, see Using the External Dependency Manager for Unity on Android.
After importing the plugin, configure the plugin as described in the plugin's README. Remember to define the Web App Client ID to the Oauth 2.0 Web client ID created earlier.
Play Games Services Sign In will automatically attempt to sign in at application launch, and all the client needs to do is to listen for successful login and pass the authentication code to the server for validation. Optionally, the game may add a way for starting the login flow manually if auto-login is not sufficient.
Listen for login at game's startup:
PlayGamesPlatform.Instance.Authenticate((status) =>
{
if (status == SignInStatus.Success)
OnGPGLoggedIn();
else
{
// Optional: Make log in button visible
}
});
Optionally provide a way to log in manually:
PlayGamesPlatform.Instance.ManuallyAuthenticate((status) =>
{
if (status == SignInStatus.Success)
OnGPGLoggedIn();
else
{
// Report failure to user
}
});
Upon login, send authentication code to server:
void OnGPGLoggedIn()
{
PlayGamesPlatform.Instance.RequestServerSideAccess(forceRefreshToken: false, (string authCode) =>
{
SocialAuthManager.StartValidation(new SocialAuthenticationClaimGooglePlayV2(authCode));
});
}
Legacy Code Only
This example code assumes the game is using a legacy version of the Play Games Plugin, which is using Play Games Services Sign In v1 API. This should not be used for any new projects.
There are three basic steps you need to follow for this, and you can complete them in any way you like. They are:
SocialAuthenticationClaimGooglePlayV1
claim to the game server.The first thing that needs to be done is initialize the plugin on your game's startup:
// We only need IdToken
PlayGamesClientConfiguration configuration =
new PlayGamesClientConfiguration.Builder()
.RequestIdToken()
.Build();
PlayGamesPlatform.InitializeInstance(configuration);
Before asking for the player to log in manually, we try signing them in automatically:
PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.NoPrompt, (status) =>
{
if (status == SignInStatus.Success)
OnGPGLoggedIn();
});
Given that the automatic sign-in might not work, we have a function for manual login:
// Login
PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptAlways, (status) =>
{
if (status == SignInStatus.Success)
OnGPGLoggedIn();
else if (status == SignInStatus.AlreadyInProgress)
{
// Nothing
}
else
{
// Report failure to user
}
});
// We also have a function for manual logout in case the player
// wants to sign out of the account
PlayGamesPlatform.Instance.SignOut();
OnGPGLoggedOut();
Beware of Race Conditions
Callbacks for Authenticate
are not guaranteed to be executed on the Unity main thread. It's necessary to use proper synchronization in this case.
After that, we need to pass the pending login state to the game server:
void OnGPGLoggedIn()
{
string idToken = PlayGamesPlatform.Instance.GetIdToken();
SocialAuthManager.StartValidation(new SocialAuthenticationClaimGooglePlayV1(idToken))
}
And you're all done! Your game is successfully integrated with Google Play Games.