playgameservices / play-games-plugin-for-unity

Google Play Games plugin for Unity
Other
3.43k stars 953 forks source link

Can someone share sample for google play login? #3293

Open STFUCKDJ opened 3 months ago

STFUCKDJ commented 3 months ago

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

aronsommer commented 2 months ago

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 commented 2 months ago

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!