This article is written for developers building with Unity who want their players to be able to connect to GameSparks using a Google Play account or would like to link an existing player account to a Google Play account.


Prerequisites


Before starting, you’ll need to have the following in place:

  • A Google Developer account.
  • A Unity Project with basic GameSparks Authentication set up.
  • A GameSparks config with a Google Play integration.


The set up work falls into four stages:

  • Setting up a Google Play App
  • Integrating with GameSparks
  • Getting Google Play and Android SDK Set up in Unity

  • Publishing and Finalizing your App


Setting up a Google Play App


1. Log into the Google Play Console:

    https://play.google.com/apps/publish/


2. In the Google Play Console, create an Application and a Game. In your Game, go to Linked Apps and link a Web app:


3. Save and continue. You now need to Authorize your app.


4. Click Authorize your App:

  • You’ll see the hostname:

        https://www.gamesparks.com

  • And a redirect URI:

       https://www.gamesparks.com/oauth2callback


5. Click confirm. This will give your Client ID. You’ll see a link to the API Console in Google on this page, which you can click to go and get your Client Secret:

  • Make a note of both your Client ID and Client Secret - you'll need them for the next stage when you integrate with GameSparks.


NOTE: If you lose your Client ID or Client Secret and you need to get into the API Console again, you can access it from the Google Play Console by going to Games Services > {Their Game} > Linked Apps > {Their Web Application}. You can then scroll down to OAuth2 Client Secret, which gives a link to the API Console.  


Integrating with GameSparks


You must ensure that your Google app has been added to GameSparks as a 3rd-party integration in order to authenticate with Google Play. If you haven't selected Google as a GameSparks Integration when you first created your game in GameSparks, you can add it at any time:


1. Navigate to Configurator > Game Overview > Edit > Features & Integrations and tick the Google option under Integrations.


2. Once you have the Google Integration added, navigate to Configurator > Integrations > Google > Edit.


3. Now fill in the App ID, Client ID and Client Secret, which you noted down in the previous section.


Getting Google Play and Android SDK Set up in Unity


Resources:

  • You can follow the Android SDK setup instructions here:

       https://docs.unity3d.com/Manual/android-sdksetup.html 


  • You can download the latest Google Play plugin for Unity here:

        https://github.com/playgameservices/play-games-plugin-for-unity


Once you've added the Google Play package and set up the Android SDK:


1. In Unity, first go into Build Settings and ensure your Platform is set to Android.


2. Now go to Window > Google Play Games > Setup > Android Setup…

  • You can get your Resources Definition from your Google Play Console. In order to get this, you need to set up an Achievement, Leaderboard, or Event in your game on the Google Play Console and then click Get Resources in the Achievement/Leaderboard/Event screen. You can then Copy & Paste the contents into Resource Definitions under the Unity > Android Configuration and the Client ID that was created when you linked your app to GameSparks. This will create a new CS script in the directory you specified.


3. Create a GoogleAuth script and attach it to an object in your scene. Add in the following code:


using UnityEngine;
using UnityEngine.UI;
using GameSparks.Api.Requests;
using GooglePlayGames;
using GooglePlayGames.BasicApi;

public class GoogleAuth : MonoBehaviour
{

    public Text log;

    public void Start()
    {
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().RequestServerAuthCode(false).Build();

        PlayGamesPlatform.InitializeInstance(config);
        //Initialize Google Play
        PlayGamesPlatform.Activate();

    }

    //Called from a UI Button
    public void GooglePlayStart()
    {
        //Start the Google Play Overlay Login
        PlayGamesPlatform.Instance.Authenticate((bool success) => {
            //Do something depending on the success achieved
            log.text = log.text + "\nAuthenticated? " + PlayGamesPlatform.Instance.IsAuthenticated();
        });
    }


    //Called from a UI Button
    public void GooglePlayAuthentication()
    {
        //We set the access code to the newly added built in function in Google Play Games to get our code
        new GooglePlusConnectRequest().SetCode(PlayGamesPlatform.Instance.GetServerAuthCode()).SetRedirectUri("https://www.gamesparks.com/oauth2callback").Send((googleAuthResponse) =>
        {
            if (!googleAuthResponse.HasErrors)
            {
                Debug.Log(googleAuthResponse.DisplayName + " Logged In!");
                log.text = log.text + "\n" + googleAuthResponse.DisplayName + " Logged In!";
            }
            else
            {
                Debug.Log(PlayGamesPlatform.Instance.GetServerAuthCode());
                log.text = log.text + "\n" + googleAuthResponse.JSONString;
            }
        });
    }
}


The above code performs 3 actions:

  • The Start function builds your PlayGames config and is set to request a Server Auth Code.
  • The GooglePlayStart function authenticates your Google Play instance and outputs true or false based on it’s success.
  • The GooglePlayAuthenticate function then sends a GooglePlayConnectRequest, using the Server Auth Code and the Redirect URI previously configured and prints a response.

 

4. Now:

  • Create 2 buttons (Google Start and Google Auth) and a text box to log the result of the operation.
  • Add the GameObject containing your GoogleAuth script to the OnClick of your button and point the function to the respective function in the script and drag the text object you created for your output into the GoogleAuth script.

 

5. With all of this done, you can now build your APK. Go into Build Settings and click on Player Settings…:

  • First, expand Other Settings and set the Package Name, Version and Bundle Version Code.
  • Next expand Publishing Settings and check Create a new keystore… Set a keystore password and confirm it. Click on the Alias drop-down and click Create a new key and fill out each of the details:
    • Keep this keystore safe because it is used to sign your app.


Publishing and Finalizing your App


Once you have built your Unity application and have the APK, the Unity side of the process should be done. You can now go the the Google Play Console and publish and finalize your app.


1. Go through the steps to publish your game - age ratings, pricing, and so on.


NOTE: When you get to this section, just click opt-out unless you specifically want to use Google’s App Signing. After signing up, you cannot back out so we would recommend that you consider this option carefully, unless you are positive you want to use it. You can also always opt in later if necessary.


2. Once you have your APK built, go to Release Management in your Play Dashboard and create an Alpha release. Here you can upload your APK to appear on the Play Store. You will also need to set Testers and each tester will have to navigate to the Opt-In URL in the Manage Testers screen to access the game on the Play Store.


IMPORTANT: All sections must be completed before you can make a release, so make sure you can see a green tick beside all sections of your game and app in your Play Console.


3. Finally you need to Link your Game and App in a similar way to how you linked the Game to GameSparks:

  • Go to Linked Apps > Link another app.
  • This time choose Android, fill in the package name of your app, and publish the changes.


Now, if you download the app from the app store or put the APK directly onto your phone, you should be able to send a successful GooglePlayConnectRequest by pressing the Google Start button to authenticate Google Play and then press the Google Play button to send the request.