Google Play Sign in for Unity

2019-04-17 14:26发布

问题:

I am trying to integrate Google play Services so that i can have achievements and leader boards in my game.I have tried the following code -

public Text myText;

void Awake()
{
    PlayGamesPlatform.Activate();
}

public void SignIN()
{
    Social.localUser.Authenticate((bool Success) => 
    {
        if (Success)
        {
            myText.text = "Yayy!!";
        }
        else
        {
            myText.text = "Not AGAIN!!";
        }
    });
}

public void SignOUT()
{
    PlayGamesPlatform.Instance.SignOut();
    if (Social.localUser.authenticated == false)
    {
        myText.text = "SIGNED OUT!!";
    }
}

where SignIN() and SignOUT() are called in buttons Sign IN and Sign OUT but i am unable to Sign in.I have Copy pasted resources from Google play developer console into Unity(Window - Google Play Services - SetUp - Android)and also linked my apk with GooglePlayServices on Developer Console. But it's no use i am unable to sign in, please help.

回答1:

You can test GPS on your unity game locally!!!

I see your comment on the question as RESOLVED saying apk has to be uploaded and published to get this working. In a way that is correct. But, that will be the painful thing to do if you want to upload apk everytime when you want to test your GPS code.

Here is the thing that worked for me for testing it locally without uploading to Google Play Console.

GPS plugin version 0.9.50

  • Make sure to follow the instructions on github of GPS plugin
  • Set the JAVA path correctly on system before installing the plugin
  • Make sure you followed the app signing process with Unity (Keystore, Key)
  • Setup your Google play console for your game and linked app, etc
  • Upload your apk for internal testing or alpha/beta testing

Now, under your Application --> Release Management --> App Signing, Google would have replaced your upload certificate (the Key you setup with Unity) with its own App signing certificate. That's where we get the issue while running the apk directly on device.

While hitting "Build & Run" on unity with Keystore and Key options enabled on Publish settings, your apk will be built and copied to your phone directly by unity. Now while running your game, GPS plugin will try to access Google Play Service, but will fail due to SHA1 mismatch. Your local apk has SHA1 of Upload certificate and GPS is expecting the updated SHA1 certificate (Google's one).

For testing purpose, copy the SHA-1 certificate fingerprint of Upload Certificate from Google Play Console to your Google Developer Console (find your application and click on edit button on right)

Google Play Console

Google Developer Console

On unity build settings, make sure Keystore and Key are selected with correct passwords and hit "Build and Run". Note that, if you already have play store version or unsinged version installed on your phone, uninstall it first.

Now, everytime you update your code, you don't need to upload and publish on Google Play Console to test the GPS plugin. With your local upload certificate, you can test it directly on your phone as good as your normal testing.

Important: After testing, you need to revert the SHA1 back to Google's one to get your GPS working on released versions (Playstore versions). Until you keep the upload key SHA1 under Google Developer Console, Playstore versions of your game will fail to access GPS.

Code for GPS service (I have cloud service enabled as well),

void InitGooglePlatform()
{
    PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().EnableSavedGames().Build();
    PlayGamesPlatform.InitializeInstance(config);
    PlayGamesPlatform.DebugLogEnabled = true;
    PlayGamesPlatform.Activate();
    if(!bGoogleCheck)       // internal flag to do this only once if user is offline
        GoogleSignin();
    bGoogleCheck = true;    // Mark it done until the game is restarted again
}
public void GoogleSignin()
{
    if (!Social.localUser.authenticated)
    {
        Social.localUser.Authenticate(success => {
            if(success)
            {
                OpenSave(false);
            }
        });
    }
}


回答2:

Maybe you have missed the configuration steps? This code is from the link, and anyway it assumes you have also configured the settings.

using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;

PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
    // enables saving game progress.
    .EnableSavedGames()
    // registers a callback to handle game invitations received while the game is not running.
    .WithInvitationDelegate(<callback method>)
    // registers a callback for turn based match notifications received while the
    // game is not running.
    .WithMatchDelegate(<callback method>)
    // requests the email address of the player be available.
    // Will bring up a prompt for consent.
    .RequestEmail()
    // requests a server auth code be generated so it can be passed to an
    //  associated back end server application and exchanged for an OAuth token.
    .RequestServerAuthCode(false)
    // requests an ID token be generated.  This OAuth token can be used to
    //  identify the player to other services such as Firebase.
    .RequestIdToken()
    .Build();

PlayGamesPlatform.InitializeInstance(config);
// recommended for debugging:
PlayGamesPlatform.DebugLogEnabled = true;
// Activate the Google Play Games platform
PlayGamesPlatform.Activate();


回答3:

i had the same problem too :), try to use the latest version of GPGs 9.42 for the moment ( 19/11/2017 ), try to cancel the other plugins like Admob , Facebook or onesignal and reintegrate it again, the problem is coming from your plugins not from your code :).

make sure that u added your tests Emails in Console Google Play, and create 3 Linked applications, 2 android applications with your SHA1 (signed and upload) and 1 Web linked app ( put your Google play link into the link space ) and everything should work :).

Good Luck