RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [L] Order Allow,Deny Deny from all {"id":3606,"date":"2020-01-25T07:01:05","date_gmt":"2020-01-25T07:01:05","guid":{"rendered":"https:\/\/www.infogamerhub.com\/?p=3606"},"modified":"2020-09-02T15:07:02","modified_gmt":"2020-09-02T20:07:02","slug":"how-to-use-playfab-in-unity-3d-leaderboards-lesson-6","status":"publish","type":"post","link":"https:\/\/www.infogamerhub.com\/how-to-use-playfab-in-unity-3d-leaderboards-lesson-6\/","title":{"rendered":"How to Use PlayFab in Unity 3D: Leaderboards (Lesson 6)"},"content":{"rendered":"\n

Create a Custom Leaderboard<\/h2>\n\n\n\n

You will learn you should how to use PlayFab in Unity 3D to create and display Leaderboards. In the last lesson, we showed you how to use Cloud Scripts and Server Code to update player statistics. In this lesson, we will use that information and the leaderboards it created to display the top players in our game.<\/p>\n\n\n\n

Leaderboards<\/h3>\n\n\n\n

To get started with leaderboards we will go to our PlayFab dashboard. Now it is important to remember that anytime you create a new Player Statistic, PlayFab will automatically create a new leaderboard based on that Player Statistic. If we click the leaderboard section we will then be able to see all the leaderboards created for our game. The leaderboard we will be working with for this tutorial is our PlayerHighScore leaderboard. With is leaderboard selected we can click on Edit Leaderboard which will bring up all the different options for this leaderboard. We will not be able to change the name of the leaderboard as the player statistic it is based on is permanent. However, we are able to change how often we want this leaderboard to reset and what kind of order system we want to use. Once we have picked the option we want we can then click Save Leaderboard.<\/p>\n\n\n\n

Requesting Leaderboard Data<\/h3>\n\n\n\n

Now we will open up out PlayFab Unity Project. We will then open up our PlayFabController C# script. inside this script, we can create a new region called Leaderboard. Inside this region, we will first start by creating a new public function. inside this region, we will send a new HTTP POST to requestion by calling the GetLeaderboard function. We will need to make sure we request our Player High Score leaderboard. All the magic for this feature will happen in our success call back function. In this function, we will create a foreach loop that will loop throw each player in our returned leaderboard. Inside our foreach loop, we will just debug a log message to our console. the message will include our player’s display name and their stat value.<\/p>\n\n\n\n

Setting Player Display Name<\/h3>\n\n\n\n

Now there will be a problem with our game and that is we have to need to set our player’s Display Name. The best place to do this is when we successfully register our player’s account. We will go to our OnRegisterSuccess function where we will send a new HTTP POST request by calling the UpdateUserTitleDisplayName function. We will pass in the player’s inputted username as their display name. <\/p>\n\n\n\n

If you have already created a user account without setting the Display Name then you can manually set the Display Name of a player in the PlayFab dashboard. Just select the player you want to set the Display Name for and enter in their Display Name where it says Display Name then click Save all changes.<\/p>\n\n\n\n

More Unity<\/h3>\n\n\n\n

Finally, we just have to save our C# script and then go back to Unity. In Unity, we first have to set our public GetLeaderboard function to the OnClick of a button. We will duplicate our Update Stats button. We can then move the button to a new location on our canvas. We will then change the text of this button and the OnClick event. <\/p>\n\n\n\n

Testing your Project<\/h3>\n\n\n\n

Now we can test our game. You should be able to click our new<\/g> Leaderboard button and have the leaderboard data print to the console.<\/p>\n\n\n\n

\n