Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity-谷歌Play服务排行榜获得用户排名的问题_C#_Unity3d_Google Play Services_Leaderboard - Fatal编程技术网

C# Unity-谷歌Play服务排行榜获得用户排名的问题

C# Unity-谷歌Play服务排行榜获得用户排名的问题,c#,unity3d,google-play-services,leaderboard,C#,Unity3d,Google Play Services,Leaderboard,我对谷歌Play服务排行榜有意见,我已经被困了好几天了 我在用电脑 我正在尝试获取特定级别的Userrank。因此,我通过LoadScores函数加载分数,这似乎工作正常 问题似乎是,我在“LoadScores”函数中获得了正确的排名,但是总体GetUserRank函数仍然返回rank=0,不管实际的Userrank是什么 我的猜测是,这毕竟是一个时间问题,在LoadScores函数计算出要返回的正确排名之前,就返回了排名 public int GetUserRank(字符串级别ID) { in

我对谷歌Play服务排行榜有意见,我已经被困了好几天了

我在用电脑

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

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

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

public int GetUserRank(字符串级别ID)
{
int秩=0;
字符串user=PlayGamesPlatform.Instance.localUser.id;
字符串排行榜=返回排行榜(levelID);
Social.LoadScores(排行榜,分数=>
{
如果(分数.长度>0)
{
Debug.Log(“检索的”+分数.Length+分数”);
//使用用户名筛选分数
for(int i=0;i
我通过以下方式从其他脚本引用此函数:

int-rank=PlayGamesController.Instance.GetUserRank(levelID);
非常感谢您的任何想法/提示


谢谢

听起来像是
LoadScores
async发出的,所以你
返回等级甚至在得到结果之前,所以它保持
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);
     });
 }
或者使用lambda表达式

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

谢谢你的解决方案很有魅力!
PlayGamesController.Instance.GetUserRank(levelID, rank => {
    // Do something with rank
});