How to properly integrate Google Sign-In into my Unity Android App build

Viewed 584
private string text;    

 void Start()
    {

        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().RequestIdToken().RequestServerAuthCode(false).Build();
        text = "config created";
        PlayGamesPlatform.InitializeInstance(config);
        text = text + "\n" + "config initialized";
        PlayGamesPlatform.Activate();
        text = text + "\n" + "activated";
        SignInWithPlayGames();
        text = text + "\n" + "attempted to sign in";
    }

 public void SignInWithPlayGames()
    {

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

            if (success)
            {
                string authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
                text = text + "\n" + "Auth code is: " + authCode;
                if (string.IsNullOrEmpty(authCode))
                {
                    text = text + "\n" + "Signed into Play Games Services but failed to get the server auth code.";
                    return;
                }

            }


            if (!success)
            {
                text = text + "\n" + "Failed to Sign into Play Games Services.";
                return;
            }


        });
    }

When I run this on Unity, I got

config created config initialized activate Failed to Sign into Play Games Services. attempted to signed in

which is fine since I am using my PC to test it. But I got an interesting result after I run my app on a real device. I got this:

config created config initialized activate attempted to signed in

I think it skip if (success) from public void SignInWithPlayGames() method. My app never shows Google Play UI and I am not sure now if I am using the right code.

1 Answers

I've struggled quite a bit with Google Play in the last few weeks. I did not Retrieve the ID token or auth token in the builder however. It might be causing you problems.

Also, you need to cast the social user into a google user to access its properties.

I got it to work with the following code. (If you do not get it to work with this code, the problem resides in your settings in the googleplay Plugin and/or google play console.)

    PlayGamesClientConfiguration config = new
    PlayGamesClientConfiguration.Builder()
    .Build();

    // Enable debugging output (recommended)
    PlayGamesPlatform.DebugLogEnabled = true;

    PlayGamesPlatform.InitializeInstance(config);
    PlayGamesPlatform.Activate();

    Social.localUser.Authenticate((bool success) =>
    {
        var googleUser = ((PlayGamesLocalUser)Social.localUser);

        if (googleUser.authenticated)
        {
            // access googleUser properties and store them or use them
        }
    }
Related