unity3d 获取排行榜的玩家昵称- PlayFab

ztmd8pv5  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(206)

我有一个通过PlayFab id获取用户名并将其写入变量TestName的脚本
它取自(并稍作修改)原始文档:https://learn.microsoft.com/en-us/gaming/playfab/features/data/playerdata/getting-player-profiles

// Initially TestName not set

void GetPlayerProfile(string playFabId) {
    PlayFabClientAPI.GetPlayerProfile( new GetPlayerProfileRequest() {
        PlayFabId = playFabId,
        ProfileConstraints = new PlayerProfileViewConstraints() {
            ShowDisplayName = true
        }
    },
    result => TestName = result.PlayerProfile.DisplayName,
    error => Debug.LogError(error.GenerateErrorReport()));
}

接下来,我有一个脚本,它获取排行榜数据数组,并以用户友好的格式将其写入Leaders变量

void OnLeaderboardGet(GetLeaderboardResult result)
{
    foreach (var item in result.Leaderboard)
    {
        GetPlayerProfile(item.PlayFabId);
        Debug.Log(TestName);
        Leaders += ((item.Position + 1) + ") " + TestName + ": " + item.StatValue + "\n");
    }

    LeaderboardText.text = Leaders.ToString();
    Debug.Log(Leaders);
}

该问题出现在第一个函数GetPlayerProfile(item.PlayFabId);中。该函数延迟接收用户名,并且没有时间覆盖变量TestName
结果我得到这个:

是否有快速加载排行榜的解决方案?

pbgvytdp

pbgvytdp1#

我不知道这是不是最好的办法。必要的数据没有出来的原因,似乎是因为排行榜没有正确接收。

GetLeaderboardResult _leaderBoard;

IEnumerator GetLeaderBoardData()
{
    int getLeaderBoard = 1;
    PlayFabClientAPI.GetLeaderboard(new GetLeaderboardRequest()
        {
            StartPosition = /*Some Value Setting*/,
            StatisticName = /*Some Value Setting*/,
            MaxResultsCount = /*Some Value Setting*/,
            ProfileConstraints =
            new PlayerProfileViewConstraints()
            {
                ShowLocations = true,
                ShowDisplayName = true
            }
        }, (result)=>
        {
            _leaderBoard = result;
            getLeaderBoard -= 1;
        }, (error)=>{});
    yield return new WaitWhile(()=>!(getLeaderBoard <= 0));
}

如果您按上述操作,相应的排行榜数据将被放入名为**'_leaderBoard'**的变量中。如果有其他方法异步等待值进入,您可以使用该方法。
希望这对你有帮助。:)

2nc8po8w

2nc8po8w2#

@RedStone的解决方案给了我将这行从GetPlayerProfile()移到我的GetLeaderboard()函数的想法

ProfileConstraints = new PlayerProfileViewConstraints() {
    ShowDisplayName = true
}

不再需要GetPlayerProfile()函数,可以将其删除
最后我得到了这个:

public void GetLeaderboard()
{
    Leaders = "";
    var request = new GetLeaderboardRequest
    {
        StatisticName = "BestScore",
        StartPosition = 0,
        MaxResultsCount = 10,
        ProfileConstraints = new PlayerProfileViewConstraints() {
            ShowDisplayName = true
        }
    };
    PlayFabClientAPI.GetLeaderboard(request, OnLeaderboardGet, OnError);
}

void OnLeaderboardGet(GetLeaderboardResult result)
{
    foreach (var item in result.Leaderboard)
    {
        Leaders += ((item.Position + 1) + ") " + item.DisplayName + ": " + item.StatValue + "\n");
    }

    LeaderboardText.text = Leaders.ToString();
}

到目前为止,只有第二个玩家有昵称,所以这是一个工作解决方案

相关问题