Unity使用GameCenter排行榜的坑 - SSB4455/ownArticleSome GitHub Wiki

Unity使用GameCenter排行榜的坑

首先Unity中并没有获取当前应用所有排行榜的方法 只能够自己写好排行榜的id一个一个去获取排行榜上的数据

GetLeaderboard

方法1

Social.LoadScores("leaderboardID", scores => {
		if (scores.Length > 0)
		{
			foreach (IScore score in scores) {
				if (score.userID.Equals(Social.localUser.id))
				{
					break;
				}
			}
		}
	});

这个方法实际上只能获取排行榜中排名第一的那条数据

方法2

ILeaderboard leaderboard = Social.CreateLeaderboard();
leaderboard.id = leaderboardID;
leaderboard.userScope = UserScope.Global;
leaderboard.LoadScores(success =>
{
	if (success)
	{
		Debug.Log("localuserScore " + leaderboard.localUserScore.value + " " + leaderboard.localUserScore.rank);
		var scores = leaderboard.scores;
		if (scores.Length > 0)
		{
			Debug.Log("Got " + leaderboardID + " " + scores.Length + " scores");
			foreach (UnityEngine.SocialPlatforms.IScore score in scores) {
				if (score.userID.Equals(Social.localUser.id))
				{
					
				}
				Debug.Log(score.userID + ", " + score.date + ", " + score.value + ", rank " + score.rank);
			}
		}
	}
});

如果按照上面的代码 最多只会获取排行榜中前10的数据

加上这一行

leaderboard.range = new Range(1, 100);

最多可以获取前100的数据

但是这个range不能小于1 不能大于100

也就是说100名之后的数据还是没法获取

如果超过范围在运行时会崩溃

ReportLeaderboard

Social.ReportScore(score, leaderboardID, success => {
		Debug.Log("Repore " + leaderboardID + " score: " + score + (success ? " Success" : "Failed"));
	});

不论是否成功success永远为true 自己确保leaderboardID别填错了吧