Documentation

​
​

Development

User Acquisition

Monetization

Industry

Leaderboards

Unity SDK

Admin API

Client API

Open Unity Dashboard

Leaderboards

LiveOps
​
​
Leaderboards
  • Overview
  • Get started
  • Concepts
  • Tutorials
    • Configuration
    • Unity SDK tutorial
    • Unity SDK sample
    • Cloud Code samples
      • Cloud Code Modules (C#)
      • Cloud Code Script (Javascript)
  • Reference
  • Privacy and consent
  1. Leaderboards

Access Leaderboards via Cloud Code C# Modules

Refer to code samples that access Leaderboards with Cloud Code C# Modules.
Read time 2 minutes
Last updated 8 months ago

You can access Leaderboards functionality using Cloud Code via the Cloud Code C# Module.
Consider the example where a player invokes a Cloud Code module. The code snippet submits the player's score, retrieves their entry (including their rank) and retrieves the top scores in the Leaderboard.
C#:
using Microsoft.Extensions.DependencyInjection;using Unity.Services.CloudCode.Apis;using Unity.Services.CloudCode.Core;using Unity.Services.Leaderboards.Model;public class LeaderboardsExample{ [CloudCodeFunction("LeaderboardsExample")] public async Task<LeaderboardResult> LeaderboardsExampleAsync( IExecutionContext context, IGameApiClient apiClient, string leaderboardId, string playerId, int score) { string accessToken = context.AccessToken; Guid projectId = Guid.Parse(context.ProjectId); var addScoreResult = await apiClient.Leaderboards.AddLeaderboardPlayerScoreAsync( context, accessToken, projectId, leaderboardId, playerId, new LeaderboardScore(score)); var getScoreResult = await apiClient.Leaderboards.GetLeaderboardPlayerScoreAsync( context, accessToken, projectId, leaderboardId, playerId); var getScoresResult = await apiClient.Leaderboards.GetLeaderboardScoresAsync( context, accessToken, projectId, leaderboardId); return new LeaderboardResult() { AddScoreResult = addScoreResult.StatusCode.ToString(), GetScoreResult = getScoreResult.Data, GetScoresResult = getScoresResult.Data }; }}public class LeaderboardResult{ public string AddScoreResult { get; set; } public LeaderboardEntryWithUpdatedTime GetScoreResult { get; set; } public LeaderboardScoresPage GetScoresResult { get; set; }}public class ModuleConfig : ICloudCodeSetup{ public void Setup(ICloudCodeConfig config) { config.Dependencies.AddSingleton(GameApiClient.Create()); }}
Admin level access to player-scoped endpoints is also available through Cloud Code. This allows you to update multiple players’ scores, or a player’s score other than the player who invoked the Cloud Code module. This is done by passing the service token to the Cloud Code module instead of the access token.
C#:
public class LeaderboardsExample{ [CloudCodeFunction("LeaderboardsExample")] public async Task<LeaderboardResult> LeaderboardsExampleAsync( IExecutionContext context, IGameApiClient apiClient, string leaderboardId, string playerId, int score) { string serviceToken = context.ServiceToken; Guid projectId = Guid.Parse(context.ProjectId); var addScoreResult = await apiClient.Leaderboards.AddLeaderboardPlayerScoreAsync( context, serviceToken, projectId, leaderboardId, playerId, new LeaderboardScore(score)); var getScoreResult = await apiClient.Leaderboards.GetLeaderboardPlayerScoreAsync( context, serviceToken, projectId, leaderboardId, playerId); var getScoresResult = await apiClient.Leaderboards.GetLeaderboardScoresAsync( context, serviceToken, projectId, leaderboardId); return new LeaderboardResult() { AddScoreResult = addScoreResult.StatusCode.ToString(), GetScoreResult = getScoreResult.Data, GetScoresResult = getScoresResult.Data }; }}

Additional resources

  • Unity Dashboard
  • Cloud Code documentation
  • Unity Authentication

Copyright © 2026 Unity Technologies
LegalPrivacy PolicyCookiesDocumentation Terms of UseDo Not Sell or Share My Personal InformationYour Privacy Choices (Cookie Settings)

"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S and elsewhere (more info here). Other names or brands are trademarks of their respective owners.

Some pages are machine-translated for convenience, and may contain inaccuracies. In the event of conflicting information, the English version is authoritative.

  • On this page
    • Additional resources


Report a problem with this page