Set up Google Play Games for Unity and sign-in

This document guides you through setting up your Unity project to use the Google Play Games plugin for Unity. You learn how to install the plugin and configure your Unity project. The document also covers how to verify the sign-in service.

Before you begin

Review the software requirements. Set up Play Console and install the Unity Hub.

Plugin Installation

To download and install the Google Play Games plugin for Unity, follow these steps in Unity Hub:

  1. Download the GitHub repo.

  2. In the current-build directory, locate the unitypackage file. This file represents the plugin. For example, it should resemble the following:

      current-build/GooglePlayGamesPluginForUnity-X.YY.ZZ.unitypackage
    

Set up Unity project

To set up a Unity project in player settings, follow these steps:

  1. Open your game project.

  2. In the Unity Hub, click Assets > Import Package > Custom Package to import the unitypackage file into your project's assets.

  3. Make sure that your current build platform is set to Android.

    1. In the main menu, click File > Build Settings.

    2. Select Android and click Switch Platform.

    3. There should be a new menu item under Window > Google Play Games. If there isn't, refresh the assets by clicking Assets > Refresh and then try setting the build platform again.

  4. In the Unity Hub, click File > Build Settings > Player Settings > Other Settings.

  5. In the Target API level box, select a version.

  6. In the Scripting backend box, enter IL2CPP.

  7. In the Target architectures box, select a value.

  8. Note the package name package_name.You can use this information later.

The player settings in your Unity project
The player settings in your Unity project.

Create a new keystore

To validate your credentials, you need a key. Follow these steps:

  1. In the Unity Hub, click File > Build settings > Player settings.
  2. In the Publishing settings section, click Keystore manager.
    1. In the Keystore manager window, click Keystore > Create new > Anywhere.
    2. Select a folder and provide a name for the keystore.
    3. In the Password box, enter a password and confirm.
    4. Click Add key.

Note the folder name. You can use this name to create a credential in Google Cloud.

Copy the Android resources from Play Console

Each achievement, leaderboard, and event you create in Play Console includes an Android resource that you use when you set up your Unity project.

To get the Android resources for your game, follow these steps:

  1. In the Google Play Console, open the game.

  2. In the Play Games Services - Configuration page (Grow > Play Games Services > Setup and management > Configuration), click Get resources.

  3. In the Resources window, click the Android(XML) tab.

  4. Select and copy the Android resources (AndroidManifest.xml) content.

Add the Android resources to your Unity project

Add the following Android resources to your Unity project:

  1. In the Unity Hub, click Window > Google Play Games > Setup > Android Setup.

    • In the Directory to save constants field, enter the folder name for the constants file.
    • In the Constants class name field, enter the name of the C# class to create, including the namespace.

      For example, if the C# class is id.cs and present under Assets > myproject > scripts > id.cs. The constants class name can be myproject.scripts.id.

    • In the Resources definition field, paste the Android resources data (AndroidManifest.xml file) you copied from the Google Play Console.

    • Optional: In the Client ID field, enter the client ID of the linked web app.

      To get the client ID for your game from Google Cloud, see Creating client IDs.

      This is only needed if you have a web-based backend for your game and need a server auth code to exchange for an access token by the backend server, or if you need an ID token for the player to make other non-game API calls.

    • Click Setup. This configures your game with the client ID and generates a C# class that contains constants for each of your Android resources.

  2. In the Unity Hub, click Window > Google Play Games > Setup > Nearby Connections Setup.

    • In the Nearby connection service ID field, enter the package_name.

      Use the same package_name that you used in set up Unity project.

    • Click Setup.

Choose a social platform

The Google Play Games Services plugin implements Unity's social interface, for compatibility with games that already use that interface when integrating with other platforms. However, some features are unique to Play Games and are offered as extensions to the standard social interface provided by Unity.

The standard API calls can be accessed through the Social.Active object, which is a reference to an ISocialPlatform interface. The non-standard Google Play Games Services extensions can be accessed by casting the Social.Active object to the PlayGamesPlatform class, where the additional methods are available.

Use the plugin without overriding the default social platform

When you call PlayGamesPlatform.Activate, Google Play Games Services becomes your default social platform implementation, which means that static calls to methods in Social and Social.Active will be carried out by the Google Play Games Services plugin. This is the behavior for most games using the plugin.

However, if for some reason you want to keep the default implementation accessible (for example, to use it to submit achievements and leaderboards to a different social platform), you can use the Google Play Games Services plugin without overriding the default one. To do this:

  1. Don't call PlayGamesPlatform.Activate
  2. If Xyz is the name of a method you need to call on the Social class, don't call Social.Xyz. Instead, call PlayGamesPlatform.Instance.Xyz
  3. Don't use Social.Active when interacting with Google Play Games Services. Instead, use PlayGamesPlatform.Instance.

That way, you can even submit scores and achievements simultaneously to two or more social platforms:

    // Submit achievement to original default social platform
    Social.ReportProgress("MyAchievementIdHere", 100.0f, callback);

    // Submit achievement to Google Play
    PlayGamesPlatform.Instance.ReportProgress("MyGooglePlayAchievementIdHere", 100.0f, callback);

Verify the sign-in service

A connection to Play Games Services is automatically attempted using the Sign-in service when your game is opened. If the connection succeeds, your game displays a sign-in prompt and is ready to use the Google Play Games Services plugin for Unity.

If a user has never used Google Play Games Services on their device, they are automatically taken through one-time setup screen to create a Play Games account.

In the Start method of your script, listen to the result of the automatic sign-in attempt, fetch the authentication status, and disable Play Games Services features if the user is not signed in.

If the Unity plugin version is before v11, you can't use the sign-in feature.

    using GooglePlayGames;

    public void Start() {
      PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication);
    }

    internal void ProcessAuthentication(SignInStatus status) {
      if (status == SignInStatus.Success) {
        // Continue with Play Games Services
      } else {
        // Disable your integration with Play Games Services or show a login button
        // to ask users to sign-in. Clicking it should call
        // PlayGamesPlatform.Instance.ManuallyAuthenticate(ProcessAuthentication).
      }
    }

The result code is an enum that you can use to identify the reason for a sign-in failure.

If you prefer using Unity's Social platform, you can alternatively use the following code:

  using GooglePlayGames;

  public void Start() {
    PlayGamesPlatform.Activate();
    Social.localUser.Authenticate(ProcessAuthentication);
  }

You cannot make any Google Play Games Services API calls until you get a successful return value from Authenticate. As a result, we recommend that games display a standby screen until the callback is called to ensure users can't start playing the game until authentication completes.

Use Play App Signing

Google manages and protects your app's signing key using Play App Signing. You can use Play App Signing to sign optimized, distribution of Android APP Bundle(AAB) files. Play App Signing stores your app signing key on Google's secure infrastructure. To use Play App Signing, you have to first create and download an AAB file from Unity Hub. You can then upload the AAB file to Play Console and create an internal testing release.

Create an AAB file

To create an AAB file in Unity Hub, follow these steps:

  1. In the Unity Hub, click File > Build settings.
  2. Select Build App Bundle ( Google Play ).

    For more information, see Android Build Settings reference.

  3. Click Build.

  4. Download the AAB file from the Unity Hub.

Create an internal testing release

To create an internal testing release and add testers in Play Console, perform the following steps:

  1. In the Google Play Console, select a game.
  2. Navigate to the Test and release page (Testing > Internal testing).
  3. Click Upload and select the AAB file.
  4. In the Release details field, enter a name.
  5. Click Next and review the release details.
  6. Click Save and publish.
  7. On the Testers tab, click Create email list to add up to 100 testers.

    For more information, see Internal test: manage up to 100 testers.

  8. In the Feedback URL or email address, enter a feedback URL or and email address to provide feedback.

  9. Click Save.

Verify your App signing credentials

  1. In the Google Play Console, select a game.
  2. Navigate to the Test and release page (Setup > App signing).
  3. Verify your App signing credentials.

Build and run the project

You can build and run the game project at this point. When game starts, you'll see the automatic sign-in attempt.

You need a physical Android-powered device with USB debugging enabled or an emulator that can run the developed project.

Retrieve server authentication codes

In order to access Google APIs on a backend web server on behalf of the current player, you need to get an authentication code from the client application and pass this to your web server application. The code can then be exchanged for an access token to make calls to the various APIs. For information about the workflow, see sign in with Google for web.

To get the server side access code:

  1. Configure the web client id of the web application linked to your game in the Play Console.

  2. Call PlayGamesPlatform.Instance.RequestServerSideAccess once the player is authenticated to get the server side access code.

  3. Pass this code to your server application.

  PlayGamesPlatform.Instance.RequestServerSideAccess(
    /* forceRefreshToken= */ false,
    code -> {
      // send code to server
    });

Set up and add features