Unity using Google Play Games Services Plugin

This setup is for a absolut basic setup, using the Google Play Games Login only. Assuming you build your app from scratch.

💖 Health Check / Goal Project

  • Windows 10

  • Unity 2020.3.14f1 (07, August 2021)

    • Module: Android Build Support

    • Module: iOS Build Support (only to avoid a bug using the GPGS Plugin)

    • 2D Template

  • Play Games Plugin for Unity 0.12.10

  • 2D Mobile Android App

  • Minimum SDK: Android 30

Set up a Unity Project

Set up Android

Build Settings

  • Check Build App Bundle (Google Play)

  • Click Switch Platform

Project Settings > Player

API Level 30 is manadatory due to newest google restrictions

Windows To update to API Level 30, ensure to run Unity Hub as administrator.

  • Other Settings

    • Identification

      • Override Default Package Name > Check it

      • Override Default Package Name > Set your <your.package.name>

      • Version > Set your app version 0.1.1

      • Bundle Version Code> 1

      • Minimum API Level > Android 11.0 (API Level 30)

    • Configuration

      • Scripting Backend > Select IL2CPP

      • Target Architectures > Check ARM64

If you use Google Managed Keys, there is no need to handle any Upload or App Signing Key.

  • Create your first <myappbundle>.aab bundle

    • Build Settings > Build

USB Debugging: Setup

Setup USB Debugging with your OS (e.g. Android)

Install Unity Play Games Services Plugin

Install the latest Play Games Plugin For Unity in your Unity project.

Google Play Console App: Set up app

Create the App

  • Go to Google Play Console > Create App > Enter your <appName>

    • Select your default language

    • Choose app type (probably Game)

    • Choose free app or paid app

    • Check both Checkboxes

Upload your .aab

  • Testing > Internal Testing

    • Testers > Add your google mail as a test account

    • Create new release

      • App Integrity > Continue

This create the App Signing Key managed by google.

Accessible unter Setup > Integrity

  • Testing > Internal Testing

    • App Bundles

      • Upload your .aab bundle created by Unity

This first upload creates the upload key managed by google.

  • Testing > Internal Testing

    • Save > Review Release > Start Rollout to Interal Testing

    • Testers > Copy Link

      • Open Link (Google Play Store and install the app)

Ensure that you removed previous versions of this app on your mobile device.

From here on, you set up an empty Android app and made it avaiable for internal testing. No Google Play Services used yet.

Google Play Services Plugin: Setup

  • Install the GPGS Plugin for Unity.

    • Simply download the .package file and double click it while unity project is open

  • Apply Google Play Services Resource

    • Go to Google Play Console > Your App > Play Games Services > Configuration > Credentials > Get resources (on the right hand side)

      • Copy its content

    • Unity > Window > Google Play Games > Setup > Android Setup ...

      • insert copied resource content

      • Click Setup

The resource file should look similar to the following:

<?xml version="1.0" encoding="utf-8"?>
<!--Google Play game services IDs. Save this file as res/values/games-ids.xml in your project.-->
<resources>
  <!--app_id-->
  <string name="app_id" translatable="false">1234567890123</string>
  <!--package_name-->
  <string name="package_name" translatable="false">your.package.name</string>
</resources>

As long as you don't use any services such as achievements or leaderboard, no constant file will be created.

Implement the code

using UnityEngine;
using System;
using GooglePlayGames;
using GooglePlayGames.BasicApi;


public class PlayGames : MonoBehaviour
{
    public int playerScore;
    public static PlayGamesPlatform platform;

    void Start()
    {
        if (platform == null)
            {
                PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
                PlayGamesPlatform.InitializeInstance(config);
                PlayGamesPlatform.DebugLogEnabled = true;
                platform = PlayGamesPlatform.Activate();
            }

            PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptOnce, (result) =>
            {
                Debug.LogError($"[CanPromptOnce]: {JsonConvert.SerializeObject(result)}");
            });

            Social.Active.localUser.Authenticate(success =>
            {
                if (success)
                {
                    Debug.Log("Logged in successfully");
                }
                else
                {
                    Debug.Log("Login Failed");
                }
            });
    }
}

Allow Unity with Java Access

Currently there is a bug with building the app. It demands access to JAVA which it should have.

Reload the JDK and SDK in Unity

To fix it, deselect the JDK and the SDK in Unity > Preferences , unselected them and then select them in order again. It will recompile and the build will run in most cases. In my case it didn't, hence I did the second solution.

Last updated