Reputation: 439
I would like to know if is it possible to retrieve rank of the actual player, in a leaderboard data?
I want to do this for a custom leaderboard UI.
I'm using unity and the google play games
Upvotes: 2
Views: 2179
Reputation: 125455
Not sure how to obtain the rank with the official google-play-games SDK if that's what you are using.
With Unity's Social
API, you can obtain the player's rank with IScore.rank
. First, load the scores with Social.LoadScores
which gives you array of IScore
. Loop over it and compare the IScore.userID
until you find the user id you want to get the rank for then get the IScore.rank
.
void GetUserRank(string user, Action<int> rank)
{
Social.LoadScores("Leaderboard01", scores =>
{
if (scores.Length > 0)
{
Debug.Log("Retrieved " + scores.Length + " scores");
//Filter the score with the user name
for (int i = 0; i < scores.Length; i++)
{
if (user == scores[i].userID)
{
rank(scores[i].rank);
break;
}
}
}
else
Debug.Log("Failed to retrieved score");
});
}
Usage:
int rank = 0;
GetUserRank("John", (status) => { rank = status; });
Debug.Log("John's rank is: " + rank);
Or
string id = Social.localUser.id;
//string id = PlayGamesPlatform.Instance.localUser.id;
int rank = 0;
GetUserRank(id, (status) => { rank = status; });
Debug.Log(id + "'s rank is: " + rank);
Of course, you will have to do some authentication stuff because you can o this.
Upvotes: 3