> For the complete documentation index, see [llms.txt](https://justinmind-apps.gitbook.io/unity-using-gpgs-plugin/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://justinmind-apps.gitbook.io/unity-using-gpgs-plugin/master.md).

# Unity using Google Play Games Services Plugin

## 💖 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`

{% hint style="warning" %}
**API Level 30 is manadatory** due to newest google restrictions
{% endhint %}

{% hint style="danger" %}
**Windows** To update to API Level 30, ensure to run Unity Hub as administrator.&#x20;
{% endhint %}

* **`Other Settings`**
  * `Identification`&#x20;
    * `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)&#x20;
  * `Configuration`
    * `Scripting Backend` > Select `IL2CPP`
    * `Target Architectures` > Check `ARM64`

{% hint style="info" %}
`If you use Google Managed Keys, there is no need to handle any Upload or App Signing Key.`
{% endhint %}

* Create your first `<myappbundle>.aab` bundle
  * `Build Settings` > `Build`

### USB Debugging: Setup

[Setup USB Debugging with your OS (e.g. Android)](https://docs.unity3d.com/Manual/UnityRemote5.html)

### Install Unity Play Games Services Plugin

Install the latest [Play Games Plugin For Unity](https://github.com/playgameservices/play-games-plugin-for-unity/tree/master/current-build) in your Unity project.

## Google Play Console App: Set up app

### Create the App

* Go to [`Google Play Console`](https://play.google.com/) > Create App > Enter your `<appName>`
  * &#x20;Select your default language
  * Choose app type (probably Game)
  * Choose `free app` or `paid app`
  * Check both Checkboxes

### Upload your .aab

* `Testing` > `Internal Testing`&#x20;
  * `Testers` > Add your `google mail` as a test account
  * `Create new release`
    * `App Integrity` > `Continue`

{% hint style="info" %}
This create the App Signing Key managed by google.

Accessible unter `Setup` > `Integrity`
{% endhint %}

* `Testing` > `Internal Testing`&#x20;
  * App Bundles
    * Upload your .aab bundle created by Unity

{% hint style="info" %}
This first upload creates the upload key managed by google.
{% endhint %}

* `Testing` > `Internal Testing`&#x20;
  * `Save` > `Review Release` > `Start Rollout to Interal Testing`
  * `Testers` > `Copy Link`
    * `Open Link (Google Play Store and install the app)`

{% hint style="info" %}
Ensure that you removed previous versions of this app on your mobile device.
{% endhint %}

{% hint style="info" %}
From here on, you set up an empty Android app and made it avaiable for internal testing. No Google Play Services used yet.
{% endhint %}

## Google Play Services Plugin: Setup

* Install the [GPGS Plugin for Unity](https://github.com/playgameservices/play-games-plugin-for-unity/tree/master/current-build).
  * 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)&#x20;
    * 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:

```markup
<?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>
```

{% hint style="info" %}
As long as you don't use any services such as achievements or leaderboard, no constant file will be created.
{% endhint %}

### Implement the code

```csharp
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.&#x20;

### 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.
