PlayGamesPlatform.Instance.LoadScores 不返回任何内容

PlayGamesPlatform.Instance.LoadScores not returning anything

提问人:Casadev 提问时间:10/9/2023 最后编辑:Casadev 更新时间:10/9/2023 访问量:63

问:

在我的游戏中,我使用 Google Play 游戏服务来实施在线排行榜,供玩家在完成关卡后提交玩家分数。 当我在手机上测试游戏并在关卡结束时将我的分数提交到排行榜时,回调返回为成功,但这就是我的问题出现的地方。

当我使用默认的谷歌UI查看排行榜时

    public void ShowDefaultLeaderboard()
    {
        PlayGamesPlatform.Instance.ShowLeaderboardUI();
    }

分数确实出现了,如UI图像所示: downtownleaderboard forestleaderboard

并且还显示在我的 logcat 中:reportscorelogcat

但是当我在排行榜上点击我的名字时,也会出现:Nointernetconnection

我的主要问题是,当我尝试打开我的自定义 UI 并使用 PlayGamesPlatform.Instance.LoadScores() 方法检索排行榜数据以放入我自己的 UI 中时,它在我的 logcat 中给了我这个错误: loadscoresfailederror

当我查看 Google Play 游戏控制台的排行榜部分时,没有条目,只有破折号出现。控制台排行榜

这是我制作的 LeaderboardManager 脚本,我已经有几个人查看了它并确认一切看起来都很好并且应该可以工作,所以我觉得这更像是我的脚本之外的问题,而是我的脚本的问题,但如果有人也想看看它, 在这里:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;
using UnityEngine.SceneManagement;
using TMPro;

public class LeaderboardManager : MonoBehaviour
{
    public static LeaderboardManager Instance;

    private string leaderboardID = "level1leaderboardID";
    private LeaderboardStart leaderboardStart = LeaderboardStart.TopScores;
    private int scoresToDisplay = 100;
    private LeaderboardCollection leaderboardType = LeaderboardCollection.Public;
    private LeaderboardTimeSpan leaderboardTimeSpan = LeaderboardTimeSpan.AllTime;

    [SerializeField] private LeaderboardEntryCard template;
    [SerializeField] private Transform container;
    [SerializeField] private GameObject uploadResult;
    [SerializeField] private GameObject uploadButton;
    [SerializeField] private TMP_Dropdown dropMenu;

    private void Awake()
    {
        if(Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    public void ShowDefaultLeaderboard()
    {
        PlayGamesPlatform.Instance.ShowLeaderboardUI();
    }

    public void RefreshLeaderboard()
    {
        LoadLeaderboard();
    }

    private void LoadLeaderboard()
    {
        PlayGamesPlatform.Instance.LoadScores(leaderboardID,
            leaderboardStart,
            scoresToDisplay,
            leaderboardType,
            leaderboardTimeSpan,
            (LeaderboardScoreData data) => {

                //Making list of userIDs
                List<string> userIDs = new List<string>();

                //Mapping userIDs to scores
                Dictionary<string, IScore> userScores = new Dictionary<string, IScore>();
                for (int i = 0; i < data.Scores.Length; i++)
                {
                    IScore score = data.Scores[i];
                    userIDs.Add(score.userID);
                    userScores[score.userID] = score;
                }

                //Mapping userIDs to usernames
                Dictionary<string, string> userNames = new Dictionary<string, string>();
                Social.LoadUsers(userIDs.ToArray(), (users) =>
                {

                    for (int i = 0; i < users.Length; i++)
                    {
                        userNames[users[i].id] = users[i].userName;

                        Debug.Log($"User {users[i].id}: {users[i].userName}");
                    }

                    //Looping through the data so that scores and users align correctly, then instantiating the entry cards
                    for (int i = 0; i < data.Scores.Length; i++)
                    {
                        IScore score = data.Scores[i];

                        string userName = userNames[score.userID];

                        int rank = score.rank;

                        var card = Instantiate(template, container);
                        card.gameObject.SetActive(true);
                        card.SetEntry(rank, userName, score.value);
                    }

                });

            });
    }

    private string GetLeaderboardCode()
    {
        int currentScene = SceneManager.GetActiveScene().buildIndex;

        if (currentScene == 2)
        {
            return "level1leaderboardID";
        }
        else if(currentScene == 3)
        {
            return "level2leaderboardID";
        }
        else if(currentScene == 4)
        {
            return "level3leaderboardID";
        }
        else
        {
            return null;
        }
    }


    public void PostScoreToLeaderboard()
    {
        long leaderboardScore = long.Parse(GameManager.Instance.highscoreText.text);
        Social.ReportScore(leaderboardScore, GetLeaderboardCode(), (bool success) =>
        {
            if (success)
            {
                uploadButton.SetActive(false);
                StartCoroutine(UploadResult("Successfully uploaded!"));
            }
            else
            {
                StartCoroutine(UploadResult("Failed to upload score."));
            }
        });
    }

    private IEnumerator UploadResult(string result)
    {
        uploadResult.SetActive(true);
        uploadResult.GetComponentInChildren<TextMeshProUGUI>().text = $"{result}";
        yield return new WaitForSecondsRealtime(2f);
        uploadResult.SetActive(false);
    }

    public void SetLeaderboard()
    {
        int selectedOption = dropMenu.value;

        switch (selectedOption)
        {
            case 0:
                leaderboardID = "level1leaderboardID";
                break;
            case 1:
                leaderboardID = "level2leaderboardID";
                break;
            case 2:
                leaderboardID = "level3leaderboardID";
                break;
        }
        LoadLeaderboard();
    }
}

我还检查了我的两个 SHA-1 密钥是否在我的 Google Cloud Platform 和 Play 游戏服务的配置部分中正确匹配,并且它们确实匹配。在OAuth同意屏幕方面,所有内容也都发布在云平台和Play管理中心端,OAuth2.0凭据和PGS凭据都已发布。

在这一点上,我有点纠结于该怎么做,所以任何帮助或建议将不胜感激。 我目前正在通过上传到控制台中新功能的封闭测试轨道来测试我的游戏。 我还尝试直接从 Unity 编辑器构建并运行到我的手机上。

Android unity-游戏引擎 google-play-games

评论


答: 暂无答案