Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System.Collections;
using System.Collections.Generic;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine;
using UnityEngine.SocialPlatforms;
public class LeaderboardManager : MonoBehaviour {
private void SetInteractionEnabled (bool value) {
//TODO: Add logic here that disables everything you don't want the user to interact with while the leaderboard is open.
}
public void AttemptToDisplayLeaderboard () {
if (!Social.localUser.authenticated) {
SetInteractionEnabled (false);
Social.localUser.Authenticate ((bool success) => {
if (success) {
DisplayLeaderboard ();
} else {
SetInteractionEnabled (true);
}
});
} else {
SetInteractionEnabled (false);
DisplayLeaderboard ();
}
}
//Get this value from GPGSIds.
//e.g. leaderboardID = GPGSIds.leaderboard_leaders;
private string leaderboardID = "leaderboardID";
//LeaderboardScoreData will start at rank #1.
private LeaderboardStart leaderboardStart = LeaderboardStart.TopScores;
//LeaderboardScoreData will return 100 scores.
private int scoresToDisplay = 100;
//LeaderboardScoreData will be public.
private LeaderboardCollection leaderboardType = LeaderboardCollection.Public;
//LeaderboardScoreData will show all time scores.
private LeaderboardTimeSpan leaderboardTimeSpan = LeaderboardTimeSpan.AllTime;
private void DisplayLeaderboard () {
PlayGamesPlatform.Instance.LoadScores (leaderboardID,
leaderboardStart,
scoresToDisplay,
leaderboardType,
leaderboardTimeSpan,
(LeaderboardScoreData data) => {
//List of userIDs that we're going to use to retrieve usernames.
List<string> userIDs = new List<string> ();
//Map of userIDs to their related score.
Dictionary<string, IScore> userScores = new Dictionary<string, IScore> ();
for (int i = 0; i < data.Scores.Length; i++) {
IScore score = data.Scores[i];
userIDs.Add (score.userID);
userScores[score.userID] = score;
}
//Map of userIDs to their usernames.
Dictionary<string, string> userNames = new Dictionary<string, string> ();
Social.LoadUsers (userIDs.ToArray (), (users) => {
for (int i = 0; i < users.Length; i++) {
userNames[users[i].id] = users[i].userName;
}
//The reason we loop through data.Scores here is the same reason we created the two dictionaries above.
//[data.Scores] is guaranteed to be in the correct order while [users] is not.
for (int i = 0; i < data.Scores.Length; i++) {
IScore score = data.Scores[i];
//This gets the imageIndex that can be used to grab an image from an array or dictionary.
//e.g. Sprite image = leaderboardSprites[imageIndex];
int imageIndex = (int) (score.value % imageIndexMax);
string userName = userNames[score.userID];
int rank = score.rank;
//This removes the imageIndex from the score to get the true score.
float scoreValue = ((score.value - imageIndex) / imageIndexMax);
//TODO: Use the values above to update a display in Unity.
}
});
});
}
//Score: The score you want to display on the leaderboard.
//ImageIndex: The index for which image you want to display on the leaderboard.
//I have around 212 unique images that I want to be able to display on my leaderboard.
//In order to add that index to my score I'll need add at least three trailing zeroes to my score.
private float imageIndexMax = 1000;
//Multiply the true score by imageIndexMax, add the imageIndex, and then report that new score.
//To display the image, parse the imageIndex when DisplayLeaderboard (on Line 46) is called.
public void ReportScoreToLeaderboard (float score, int imageIndex) {
long leaderboardScore = (long) (score * imageIndexMax + imageIndex);
Social.ReportScore (leaderboardScore, leaderboardID, (bool success) => {
Debug.Log ("Success!");
});
}
}
@muafakul

This comment has been minimized.

Copy link

@muafakul muafakul commented Feb 26, 2020

maybe all things will be better if you provide the complete source code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.