Skip to main content
This sample demonstrates reading from and writing to PlayFab leaderboards inside a Unity title. It covers three common leaderboard patterns: lowest score / best time (minimum aggregation), high score (maximum aggregation), and cumulative score (sum aggregation).

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 nameReset frequencyAggregation method
best_timeManuallyMinimum
high_scoreManuallyMaximum
total_scoreManuallySum

2. Add the CloudScript

Copy the contents of cloudscript.js into your title’s Cloud Script editor (Automation → CloudScript):
handlers.updateStatistic = function (args, context) {
  var event = context.playStreamEvent;

  if (event != null) {
    var request = {
      PlayFabId: currentPlayerId,
      Statistics: [{
        StatisticName: event.stat_name,
        Value: event.value
      }]
    };

    server.UpdatePlayerStatistics(request);
  }
};

handlers.updateStatistics = function (args, context) {
  var event = context.playStreamEvent;

  if (event != null) {
    var stats = event.stats;
    var statArray = [];

    for (var key in stats) {
      var value = stats[key];
      statArray.push({ StatisticName: key, Value: value });
    }

    var request = {
      PlayFabId: currentPlayerId,
      Statistics: statArray
    };

    server.UpdatePlayerStatistics(request);
  }
};
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:
FieldValue
Nameupdate_statistic
Event typeCustom Event — update_statistic
ActionExecute Cloud Script
Cloud Script functionupdateStatistic

Setup

1

Clone the repository

git clone https://github.com/Myst0gan1/PlayFab-Samples.git
2

Open the project in Unity

In Unity Hub, click Open and navigate to:
PlayFab-Samples/Samples/Unity/PlayFabLeaderboardsUnity
3

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.
4

Complete PlayFab configuration

Create the three leaderboards, upload the CloudScript, and create the automation rule as described above.
5

Open the sample scene and play

Open the scene from Assets/Scenes/, then press Play in the Unity Editor.

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.
Switching leaderboards automatically fetches fresh data, so you rarely need to press Refresh manually.

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.

Build docs developers (and LLMs) love