Requirements
Unity 2018.3+
The project was created with Unity 2018.3 and requires that version or later.
Visual Studio 2017+
Visual Studio 2017 (Update 15.7) or later is required to build the project.
PlayFab title
A PlayFab title with the required leaderboards and CloudScript configured.
PlayFab configuration
Complete all three steps before running the sample.1. Create the leaderboards
In Game Manager → Leaderboards, create the following statistics:| Statistic name | Reset frequency | Aggregation method |
|---|---|---|
best_time | Manually | Minimum |
high_score | Manually | Maximum |
total_score | Manually | Sum |
2. Add the CloudScript
Copy the contents ofcloudscript.js into your title’s Cloud Script editor (Automation → CloudScript):
updateStatistic handles a single stat update from a PlayStream event. updateStatistics handles a batch update where multiple stats are passed in a single event payload.
3. Create the automation rule
In Game Manager → Automation → Rules, create one rule:| Field | Value |
|---|---|
| Name | update_statistic |
| Event type | Custom Event — update_statistic |
| Action | Execute Cloud Script |
| Cloud Script function | updateStatistic |
Setup
Set your Title ID
Open
Assets/PlayFabSdk/Shared/Public/Resources/PlayFabSharedSettings.asset in the Unity Inspector and replace the placeholder Title ID with your own.Complete PlayFab configuration
Create the three leaderboards, upload the CloudScript, and create the automation rule as described above.
Using the sample
The sample provides a simple UI for posting and reading leaderboard entries:- Use the left / right arrows to cycle between the three leaderboard types.
- Enter a numeric value in the Value field, then press one of the Post buttons to submit that value as the corresponding score type.
- Press Refresh to reload the latest leaderboard data. Switching leaderboards also triggers a refresh automatically.
Key PlayFab features
UpdatePlayerStatistics
Posts a new statistic value for the current player. The aggregation method configured on the leaderboard determines whether the value replaces or is combined with the existing value.
GetLeaderboard
Retrieves a ranked list of players and their statistic values for a given leaderboard.
CloudScript
Server-side JavaScript that runs in response to PlayStream events, used here to update player statistics securely on the server.
PlayStream automation rules
Links custom client events to CloudScript functions, keeping statistic writes off the client and reducing cheat surface area.
