【问题标题】:Unity - Problem with Google Play Services Leaderboard get user rankUnity - Google Play 服务排行榜获取用户排名的问题
【发布时间】:2021-08-11 21:50:42
【问题描述】:

我的 Google Play 服务排行榜有问题,我已经卡了好几天了。

我正在使用Play Games Plugin for Unity

我正在尝试获取特定级别的 Userrank。因此,我通过 LoadScores 函数加载分数,这似乎工作正常。

问题似乎是,我在“LoadScores”函数中获得了正确的排名,但整体 GetUserRank 函数仍然返回 rank = 0,无论实际的 Userrank 是什么。

我的猜测是,这毕竟是一个时间问题,并且在 LoadScores 函数计算要返回的正确排名之前返回排名。

public int GetUserRank(string levelID)
{
    int rank = 0;
    string user = PlayGamesPlatform.Instance.localUser.id;
    string leaderboard = ReturnLeaderboard(levelID);
    Social.LoadScores(leaderboard, scores =>
    {
        if (scores.Length > 0)
        {
            Debug.Log("Retrieved " + scores.Length + " scores");

            //Filter the score with the user name
            for (int i = 0; i < scores.Length; i++)
            {
                if (user == scores[i].userID)
                {
                    rank = scores[i].rank;
                    // This prints out the actual rank of the user and seems to work
                    print("Rank in LoadScoresFunction: " + rank);
                    break;
                }
            }
        }
        else
            Debug.Log("Failed to retrieved score");
    });
    //Here the rank is always 0, no matter the outcome of the LoadScores Function above
    print("Rank in PlayGamesController: " + rank);
    return rank;
}

我通过以下方式从其他脚本中引用此函数:

int rank = PlayGamesController.Instance.GetUserRank(levelID);

非常感谢任何想法/提示。

谢谢!

【问题讨论】:

    标签: c# unity3d google-play-services leaderboard


    【解决方案1】:

    听起来像LoadScores异步,所以你在得到结果之前return rank;,所以它保持0

    您可能不得不使用回调而不是直接分配值:

    public void GetUserRank(string levelID, Action<int> onResult)
    {
         var user = PlayGamesPlatform.Instance.localUser.id;
         var leaderboard = ReturnLeaderboard(levelID);
         Social.LoadScores(leaderboard, scores =>
         {
             if (scores.Length > 0)
             {
                 Debug.Log("Retrieved " + scores.Length + " scores");
    
                 //Filter the score with the user name
                 for (int i = 0; i < scores.Length; i++)
                 {
                     if (user == scores[i].userID)
                     {
                         var rank = scores[i].rank;
                         // This prints out the actual rank of the user and seems to work
                         print("Rank in LoadScoresFunction: " + rank);
    
                         onResult?.Invoke (result);
                         break;
                     }
                 }
             }
             else
                 Debug.Log("Failed to retrieved score");
                 onResult?.Invoke(-1);
         });
     }
    

    然后像这样使用它使用回调方法

    private void OnReceivedResult(int rank)
    {
        // Do something with rank
    }
    
    ...
    
    PlayGamesController.Instance.GetUserRank(levelID, OnReceivedResult);
    

    或者使用 lambda 表达式

    PlayGamesController.Instance.GetUserRank(levelID, rank => {
        // Do something with rank
    });
    

    【讨论】:

    • 谢谢!您的解决方案就像一个魅力!
    猜你喜欢
    • 2019-02-21
    • 1970-01-01
    • 2014-12-19
    • 2016-10-03
    • 1970-01-01
    • 2023-04-03
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多