在 Unity 5.6 中,您可以:
void ShowScoresForToday()
{
// OutputField is a Unity text element I'm using to debug this on the device
OutputField.text = "";
var leaderboardID = "leaderboard";
var log = string.Format( "Top score for leaderboard with id: '{0}'", leaderboardID );
Debug.Log( log );
OutputField.text = log;
var leaderboard = Social.CreateLeaderboard();
leaderboard.id = leaderboardID;
leaderboard.timeScope = TimeScope.Today;
leaderboard.LoadScores( success =>
{
var scores = leaderboard.scores;
if ( scores.Length > 0 )
{
foreach ( var score in scores )
{
var logLine = string.Format( "User: '{0}'; Score: {1}; Rank: {2}",
score.userID, score.value, score.rank );
OutputField.text += "\n" + logLine;
}
}
else
{
Debug.LogError( "No scores registered" );
}
} );
}
您必须在查询之前使用 Social.CreateLeaderboard() 初始化 Leaderboard 对象
其他过滤器是,嗯,有限 - https://docs.unity3d.com/ScriptReference/SocialPlatforms.ILeaderboard.html
能够查询特定日期范围会很有趣,但不行。
我认为您不能因为 GameCenter 本身存储这些分数的方式 - https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Achievements/Achievements.html#//apple_ref/doc/uid/TP40008304-CH7-SW30
编辑:@CharlieSeligman,你问我在获得用户 ID 后如何获取用户名:
Social.LoadScores( leaderboardID, scores =>
{
var log = string.Format( "There are {0} players in the leaderboard", scores.Length );
Debug.Log( log );
var userIDs = new List<string>();
foreach ( var score in scores )
{
var idLog = string.Format( "User with id '{0}' and scored {1}, rank is {2}",
score, score.value, score.rank );
Debug.Log( idLog );
userIDs.Add( score.userID );
}
Social.LoadUsers( userIDs.Distinct().ToArray(), userProfiles =>
{
var user = userProfiles[0];
var usernameLog = string.Format( "User with id '{0}' is {1}",
user.id, user.userName );
Debug.Log( usernameLog );
} );
} );