Open STFUCKDJ opened 3 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
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!
Anything would be helpful, i was struggling for days, reading docs, chatgpt... I just cant make it work