Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can someone share sample for google play login? #3293

Open
STFUCKDJ opened this issue Mar 28, 2024 · 2 comments
Open

Can someone share sample for google play login? #3293

STFUCKDJ opened this issue Mar 28, 2024 · 2 comments

Comments

@STFUCKDJ
Copy link

Anything would be helpful, i was struggling for days, reading docs, chatgpt... I just cant make it work

@aronsommer
Copy link

Here is an example in the awake function. After login it will load the best score of the logged in user from a specific leaderboard:

#if UNITY_ANDROID
using UnityEngine;
using System.Collections;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
using GooglePlayGames.BasicApi;
using UnityEngine.SceneManagement;

public class GooglePlayGamesScript : MonoBehaviour
{
    private int TotalPoints;

    void Awake()
    {
        TotalPoints = PlayerPrefs.GetInt("TotalPoints");
        PlayGamesPlatform.Activate();
        Social.localUser.Authenticate(
            (bool success) =>
            {
                if (success)
                {
                    Debug.Log("Authentication was successful");
                    LoadTotalPoints();
                }

                if (!success)
                {
                    Debug.Log("Authentication failed");
                    LoadGameScene();
                }
            }
        );
    }

    void LoadTotalPoints()
    {
        PlayGamesPlatform.Instance.LoadScores(
            "CgkIxOLRsfsXXXXXXX",
            LeaderboardStart.PlayerCentered,
            1,
            LeaderboardCollection.Public,
            LeaderboardTimeSpan.AllTime,
            (LeaderboardScoreData data) =>
            {
                if (data == null || data.PlayerScore == null)
                {
                    Debug.Log("There was no PlayerScore in LeaderboardScoreData");
                    LoadGameScene();
                }
                if (data != null && data.PlayerScore != null)
                {
                    Debug.Log(data.Valid);
                    Debug.Log(data.Id);
                    Debug.Log(data.PlayerScore);
                    Debug.Log(data.PlayerScore.userID);
                    Debug.Log(data.PlayerScore.formattedValue);
                    if (int.Parse(data.PlayerScore.formattedValue) > TotalPoints)
                    {
                        PlayerPrefs.SetInt(
                            "TotalPoints",
                            int.Parse(data.PlayerScore.formattedValue)
                        );
                    }
                    Debug.Log("LoadTotalPoints has finished");
                    LoadGameScene();
                }
            }
        );
    }

    void LoadGameScene()
    {
        Scene scene = SceneManager.GetActiveScene();
        if (scene.buildIndex == 0)
        {
            SceneManager.LoadScene(1);
        }
    }
}
#endif

@STFUCKDJ
Copy link
Author

Here is an example in the awake function. After login it will load the best score of the logged in user from a specific leaderboard:

#if UNITY_ANDROID
using UnityEngine;
using System.Collections;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
using GooglePlayGames.BasicApi;
using UnityEngine.SceneManagement;

public class GooglePlayGamesScript : MonoBehaviour
{
    private int TotalPoints;

    void Awake()
    {
        TotalPoints = PlayerPrefs.GetInt("TotalPoints");
        PlayGamesPlatform.Activate();
        Social.localUser.Authenticate(
            (bool success) =>
            {
                if (success)
                {
                    Debug.Log("Authentication was successful");
                    LoadTotalPoints();
                }

                if (!success)
                {
                    Debug.Log("Authentication failed");
                    LoadGameScene();
                }
            }
        );
    }

    void LoadTotalPoints()
    {
        PlayGamesPlatform.Instance.LoadScores(
            "CgkIxOLRsfsXXXXXXX",
            LeaderboardStart.PlayerCentered,
            1,
            LeaderboardCollection.Public,
            LeaderboardTimeSpan.AllTime,
            (LeaderboardScoreData data) =>
            {
                if (data == null || data.PlayerScore == null)
                {
                    Debug.Log("There was no PlayerScore in LeaderboardScoreData");
                    LoadGameScene();
                }
                if (data != null && data.PlayerScore != null)
                {
                    Debug.Log(data.Valid);
                    Debug.Log(data.Id);
                    Debug.Log(data.PlayerScore);
                    Debug.Log(data.PlayerScore.userID);
                    Debug.Log(data.PlayerScore.formattedValue);
                    if (int.Parse(data.PlayerScore.formattedValue) > TotalPoints)
                    {
                        PlayerPrefs.SetInt(
                            "TotalPoints",
                            int.Parse(data.PlayerScore.formattedValue)
                        );
                    }
                    Debug.Log("LoadTotalPoints has finished");
                    LoadGameScene();
                }
            }
        );
    }

    void LoadGameScene()
    {
        Scene scene = SceneManager.GetActiveScene();
        if (scene.buildIndex == 0)
        {
            SceneManager.LoadScene(1);
        }
    }
}
#endif

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants