Sign In Register

How can we help you today?

Start a new topic

How To Login with the Official Google Play Plugin for Unity on Android

This quick guide assumes you have GameSparks already set up in your game.



Step 1: Set your project to android and in "Player Settings" set your Bundle Identifier to something appropriate:



Step 2: Grab and import the unity package from https://github.com/playgameservices/play-games-plugin-for-unity



Step 3: Create your game services on https://play.google.com/apps/publish/ 




Step 4: Link a new Web App



Step 5: Click Create Client on the next page and then take note of your client id, click "Please get it here in the APIs Console." and copy and paste you client id and secret:


into the Google Plus Integrations Tab in GameSparks:




Step 6: In Unity click "Window -> Google Play Games -> Setup -> Android Setup" Paste in your Application Id


and your Client Id you got previously into the setup box.


Step 7: Build out an APK, upload it to Google Play. Go back to your Game Services page and link it to the newly uploaded app:


The code for starting the google login process:

 

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

public class GooglePlusAuth : MonoBehaviour
{

	public Text log;

	public void Start()
	{
		//Initialize Google Play
		PlayGamesPlatform.Activate();
	}

	//Called from a UI Button
	public void GooglePlusStart()
	{
		//Start the Google Plus Overlay Login
		Social.localUser.Authenticate((bool success) => {
			//Do something depending on the success achieved
		});
	}


	//Called from a UI Button
	public void GooglePlusAuthentication()
	{
		//We set the access token to the newly added built in funtion in Google Play Games to get our token
        new GooglePlusConnectRequest().SetAccessToken(PlayGamesPlatform.Instance.GetAccessToken()).Send((googleAuthResponse) =>
		{
			if (!googleAuthResponse.HasErrors)
			{
				Debug.Log(googleAuthResponse.DisplayName + " Logged In!");
				log.text = googleAuthResponse.DisplayName + " Logged In!";
            }
			else
			{
				Debug.Log("Not Logged In!");
				log.text = googleAuthResponse.JSONString;
            }
		});
	}
}

 

For context here's what the test app I made looks like:


          


3 people have this question

 

You cannot test anything Google Play related in the Unity Editor, it must be done on device.


Shane

Hello i was trying to test this feature in Unity and got he following Error on 'Send' : (previous steps dont throw exceptions)

NullReferenceException: Object reference not set to an instance of an object

GameSparks.Core.GSConnection..ctor (GameSparks.Core.GSInstance gs, IGSPlatform gsPlatform, System.String currentUrl)

GameSparks.Core.GSInstance.NewConnection ()

GameSparks.Core.GSInstance.Send (GameSparks.Core.GSRequest request)

GameSparks.Core.GSRequest.Send (System.Action`1 callback)

GameSparks.Core.GSTypedRequest`2[IN,OUT].Send (System.Action`1 callback)


I tested this on a device.

I can login successfully using my Google account.

I am using version 5.3.3.165.

Thanks for any help.


It was my fault i hadnt setup GameSparks properly.

But now i have trouble getting the token from Google.

Any help on that?

@Paul, maybe this will help you?

https://docs.gamesparks.net/howtos/social-integration/how-to-authenticate-a-player-using-their-google-account

Am I missing something because my Credentials pane in the Google APIs API Manager does not have a client secret only a client ID?

Hello I followed above steps but not able to connect to googlesparks.

please check below logs and code

Logs:

09-25 16:25:57.522 8663 8676 I Unity : success

09-25 16:25:57.522 8663 8676 I Unity :

09-25 16:25:57.522 8663 8676 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)

09-25 16:25:57.522 8663 8676 I Unity :

09-25 16:25:57.522 8663 8676 I Unity : START

09-25 16:25:57.522 8663 8676 I Unity :

09-25 16:25:57.522 8663 8676 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)

09-25 16:25:57.522 8663 8676 I Unity :

09-25 16:25:57.582 8663 8676 I Unity : END

code :

 // Use this for initialization

 void Start ()

 {

  PlayGamesPlatform.Activate ();

  PlayGamesPlatform.DebugLogEnabled = true;

  Social.localUser.Authenticate ((bool success) => {

   // handle success or failure

   if (success) {

    Debug.Log ("success");

    Debug.Log ("START");

    new GooglePlusConnectRequest ().SetAccessToken (PlayGamesPlatform.Instance.GetAccessToken ()).Send ((googleAuthResponse) => {

     Debug.Log ("START2");

     if (!googleAuthResponse.HasErrors) {

      Debug.Log (googleAuthResponse.DisplayName + "Logged In!");

     } else {

      Debug.Log ("Not Logged In!");

     }

     Debug.Log ("END2");

    });

    Debug.Log ("END");

   } else {

    Debug.Log ("Failure");

   }

  });

 }

So I struggled a lot with it and here is the proper call to make right now :

Social.localUser.Authenticate((bool success) => {
    if (success) {
#if UNITY_ANDROID
        PlayGamesPlatform.Instance.GetServerAuthCode((status, code) => {
        new GooglePlayConnectRequest().SetCode(code).SetRedirectUri("http://www.gamesparks.com/oauth2callback").SetDisplayName(displayNameIfCreated).Send(yourcallback);
  });
}
}

 

Here is the requests / response (to proove that it works) :


GS: SEND:{"@class":".GooglePlayConnectRequest","code":"4/z2u3iGHDeXOvWX5w0ZurTD_xdE408FE5X2vgowVGHRE","redirectUri":"http://www.gamesparks.com/oauth2callback","displayName":"GooglePlayer","switchIfPossible":true,"errorOnSwitch":false,"requestId":"636136943552440820_4"}


GS: RECV:{"@class":".AuthenticationResponse","authToken":"fa391e1f-c323-4e97-9c61-7f4b803ecc63","displayName":"GooglePlayer","newPlayer":false,"requestId":"636136943552440820_4","userId":"5819ecbd9c5f348a00288a1e"}

hello do in more detail, a lot of things are not clear, more screenshots and more steps please, I really need to understand the gamespark

Hey @Thomas Tabbaza
I am doing the same thing and using your code but its giving me an error i.e. no override method take 1 argument.
I am not able to get the access token or auth code to get authenticated on GameSparks. Please explain it in details so that I am able to go through this.

 

@Robin Anttal


Sorry mate I'm not on the project that uses Gamesparks anymore. I have honestly no idea about the things they changed since a year ago.

Good luck !

Login to post a comment