【问题标题】:Retrieve player rank with google play games使用 google play 游戏检索玩家排名
【发布时间】:2018-11-13 01:56:26
【问题描述】:

我想知道是否可以在排行榜数据中检索实际玩家的排名?

我想为自定义排行榜 UI 执行此操作。

我正在使用 unity 和 google 玩游戏

【问题讨论】:

    标签: c# android unity3d google-play-games rank


    【解决方案1】:

    如果您使用的是 official google-play-games SDK,不确定如何获得排名。

    使用 Unity 的Social API,您可以通过IScore.rank 获取玩家的排名。首先,使用Social.LoadScores 加载分数,这将为您提供IScore 的数组。循环遍历它并比较IScore.userID,直到找到要获得排名的用户ID,然后获取IScore.rank

    void GetUserRank(string user, Action<int> rank)
    {
        Social.LoadScores("Leaderboard01", 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);
                        break;
                    }
                }
            }
            else
                Debug.Log("Failed to retrieved score");
        });
    }
    

    用法

    int rank = 0;
    GetUserRank("John", (status) => { rank = status; });
    Debug.Log("John's rank is: " + rank);
    

    或者

    string id = Social.localUser.id;
    //string id = PlayGamesPlatform.Instance.localUser.id;
    int rank = 0;
    GetUserRank(id, (status) => { rank = status; });
    Debug.Log(id + "'s rank is: " + rank);
    

    当然,您必须做一些身份验证工作,因为您可以这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      相关资源
      最近更新 更多