如何在 Unity 中加载乐谱?

How to load scores in Unity?

我在 Google Play 上发布了一个简单的游戏。当玩家赢得比赛时,他的分数应该在排行榜中增加。我的问题是,当玩家赢得比赛时,应用程序停止。我认为问题出在 PlayGamesPlatform.Instance.LoadScores 中,因为当我删除这部分时,没有人遇到问题。另外,我想指出并不是每个玩家在游戏中都有这样的问题。这个问题只发生在那些从未赢得比赛的人身上(他们在排行榜上没有得分)。所以,PlayGamesPlatform.Instance.LoadScores 只适用于那些已经在排行榜上有一定分数的人。

我的脚本:

PlayGamesPlatform.Instance.LoadScores(GPGSIds.leaderboard_rating, GooglePlayGames.BasicApi.LeaderboardStart.PlayerCentered, 1, GooglePlayGames.BasicApi.LeaderboardCollection.Public, GooglePlayGames.BasicApi.LeaderboardTimeSpan.AllTime, (GooglePlayGames.BasicApi.LeaderboardScoreData data) =>
{
    long score;
    if (long.TryParse(data.PlayerScore.formattedValue, out score))
        Social.ReportScore(score + 50, GPGSIds.leaderboard_rating, (bool success) => { });
    else
        Social.ReportScore(50, GPGSIds.leaderboard_rating, (bool success) => { });
});

试试这个 google sample from github

使用PlayGamesPlatform.LoadScores()

此方法直接使用PlayGamesPlatform。这种方法在访问排行榜数据时提供了额外的灵活性和信息。

 PlayGamesPlatform.Instance.LoadScores(
            GPGSIds.leaderboard_leaders_in_smoketesting,
            LeaderboardStart.PlayerCentered,
            100,
            LeaderboardCollection.Public,
            LeaderboardTimeSpan.AllTime,
            (data) =>
            {
                mStatus = "Leaderboard data valid: " + data.Valid;
                mStatus += "\n approx:" +data.ApproximateCount + " have " + data.Scores.Length;
            });

LoadScores()的参数是:

leaderboardId start position (top scores or player centered) row count leaderboard collection (social or public) time span (daily, weekly, all-time) callback accepting a LeaderboardScoreData object. The LeaderboardScoreData class is used to return information back to the caller when loading scores. The members are: 1. Id - the leaderboard id 2. Valid - true if the returned data is valid (the call was successful) 3. Status - the ResponseStatus of the call 4. ApproximateCount - the approximate number of scores in the leaderboard 5. Title - the title of the leaderboard 6. PlayerScore - the score of the current player 7. Scores - the list of scores 8. PrevPageToken - a token that can be used to call LoadMoreScores() to get the previous page of scores. 9. NextPageToken - a token that can be used to call LoadMoreScores() to get the next page of scores.

void GetNextPage(LeaderboardScoreData data)
    {
        PlayGamesPlatform.Instance.LoadMoreScores(data.NextPageToken, 10,
            (results) =>
            {
                mStatus = "Leaderboard data valid: " + data.Valid;
                mStatus += "\n approx:" +data.ApproximateCount + " have " + data.Scores.Length;
            });
    }

附加参考 How to get the user score value from Google Play Services Leaderboard

P.S.:

尝试将所有玩家的分数设置为默认 0,以便尚未玩过的玩家可以调用 loadScores。