【问题标题】:Unity GooglePlayGames LoadScores always returns nothingUnity GooglePlayGames LoadScores 始终不返回任何内容
【发布时间】:2021-03-14 16:16:06
【问题描述】:

我正在尝试使用 Google Play 游戏从排行榜中获取最高分,但它没有返回任何内容。有人能指出我正确的方向吗?

    public int WorldRecord()
{
    int topScore = 0;
    PlayGamesPlatform.Instance.LoadScores(
        GPGSIds.leaderboard_quick_fire_scores,
        LeaderboardStart.TopScores, 1,
        LeaderboardCollection.Public,
        LeaderboardTimeSpan.AllTime,
        (LeaderboardScoreData data) =>
        {
            topScore = (int)data.Scores[0].value;
        });

    return topScore;
        
}

谢谢

【问题讨论】:

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


    【解决方案1】:

    PlayGamesPlatform.Instance.LoadScores 异步运行,完成后执行传递的操作。

    但是,您的方法不会等到该请求实际完成,因此只会在 LoadScores 实际完成并提供有效结果之前返回默认值 0

    您可能更应该使用某种回调,例如

    public void WorldRecord(Action<int> onResult)
    {
        PlayGamesPlatform.Instance.LoadScores(
            GPGSIds.leaderboard_quick_fire_scores,
            LeaderboardStart.TopScores, 1,
            LeaderboardCollection.Public,
            LeaderboardTimeSpan.AllTime,
            (LeaderboardScoreData data) =>
            {
                onResult?.Invoke((int)data.Scores[0].value);
            });        
    }
    

    然后传递一个回调,当结果准备好时应该发生什么,例如

    WorldRecord(topScore => 
    {
        Debug.Log($"Top Score: {topScore}");
    });
    

    【讨论】:

      猜你喜欢
      • 2011-06-09
      • 2018-07-30
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      相关资源
      最近更新 更多