如何统一从GameCenter获取每日最高分

时间:2017-04-25 12:24:28

标签: c# unity3d unity5 game-center leaderboard

我正试图从游戏中心获取每日最佳射手数。通过使用此代码段,我可以获得所有顶级球员的得分。

Social.LoadScores ("leaderboard_id",scores => {
        if(scores.Length > 0)
        {
            Debug.Log("Got " + scores.Length + " scores");
            string myScores = "Leaderboard:\n";
            foreach (IScore score in scores)
            myScores += "AllTime" + "\t" + score.userID + " " + score.formattedValue + " " + score.date + "\n"+ " "+score.rank;
            Debug.Log(myScores);
        }

在android中,这段代码几乎可以获得前25名玩家的API调用。但是在iOS中,它只能获得前1名球员的得分,还有一件我无法找到任何东西来获取每日最高分,尽管默认情况下它会给予所有顶级球员得分。欢迎任何建议。

1 个答案:

答案 0 :(得分:2)

在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,你问我有关用户名后获取用户名的信息:

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 );
    } );
} );