Skip to content

Leaderboard

Jiufen edited this page Apr 2, 2021 · 3 revisions

We are going to take a look at the leaderboard functionality of playfab. This leaderboard only works with the playfab statistics, it doesn't work with the player data.

⏬Get Leaderboard⏬

Like the previous parts of the wiki, we will create a request for the leaderboard and then we assign an onSuccess and an Onfailure callback:

public void GetLeaderBoard(){
        var requestLeaderboard = new GetLeaderboardRequest{StartPosition = 0, StatisticName = "MaxScore", MaxResultsCount = 20};
        PlayFabClientAPI.GetLeaderboard(requestLeaderboard,OnSuccessGetLeaderboard,OnFailureGetLeaderboard);
    }

    void OnSuccessGetLeaderboard(GetLeaderboardResult result){

        foreach (PlayerLeaderboardEntry player in result.Leaderboard)
        {
            Debug.Log(player.DisplayName + ": " + player.StatValue);
        }
    }

    void OnFailureGetLeaderboard(PlayFabError error){
        Debug.LogError(error.GenerateErrorReport());
}

📐Set Display name📐

If we want to show a display name for this games(not the username of the player but a new Display name for the game) we will need to set it up to the player, we could do this when the player sing up for the first time:

private void UpdateDisplayName(){
        var requestUpdateDisplayName = new UpdateUserTitleDisplayNameRequest{DisplayName = username};
        PlayFabClientAPI.UpdateUserTitleDisplayName(requestUpdateDisplayName, OnDisplayNameSuccess, LogPlayFabError);

    }
    private void OnDisplayNameSuccess(UpdateUserTitleDisplayNameResult result){
        Debug.Log("Display Name: " + result.DisplayName);
}

Clone this wiki locally