【问题标题】:How to fetch Daily Top Score from GameCenter in unity如何统一从 GameCenter 获取每日最高得分
【发布时间】:2017-09-22 11:07:07
【问题描述】:

我正在尝试从游戏中心获取该游戏的每日最佳射手。使用这段代码sn-p,我可以得到历史最高玩家的分数。

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 中,这个 sn-p 正在为 API 调用获取几乎前 25 名的玩家。但在 iOS 中,它只给出前 1 名球员的得分,而且我找不到任何东西来获取每日最高分,尽管默认情况下它给出了历史最高球员的分数。欢迎任何建议。

【问题讨论】:

  • @Draco18s 不。这就是团结。 OP 下​​次应该标记 C#
  • @Programmer Unity 包装器中可能还有类似的字段。编译后,Swift 和 Unity 都调用相同的本机代码。这就是为什么我说相关而不是重复。

标签: c# unity3d unity5 game-center leaderboard


【解决方案1】:

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

【讨论】:

  • 有没有办法获取游戏中心玩家姓名? userID 值返回一个唯一的 id,但不是他们在 GameCenter 中的实际玩家名称
  • 谢谢亚伯 - 真是太好了!
猜你喜欢
  • 2022-01-20
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 2021-02-12
  • 1970-01-01
  • 2017-08-14
相关资源
最近更新 更多