文档

​
​

Development

User Acquisition

Monetization

工业

Leaderboards

Unity SDK tutorial

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

通过 Cloud Code C# 模块访问 Leaderboards

Refer to code samples that access Leaderboards with Cloud Code C# Modules.
阅读时间2 分钟
最后更新于 8 个月前

您可以使用 Cloud Code 通过 Cloud Code C# 模块来访问 Leaderboards 功能。
以玩家调用 Cloud Code 模块为例。通过以下代码片段,可提交玩家的分数、检索分数条目(包括排名)并检索排行榜中的最高分。
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()); }}
通过 Cloud Code 也可以对玩家作用域终端进行管理级别的访问。这样,您可以更新多个玩家的分数或单个玩家(除调用 Cloud Code 模块的玩家之外)的分数。实现方法是将服务令牌(而非访问令牌)传递给 Cloud Code 模块。
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 }; }}

其他资源

  • Unity Dashboard(Unity 后台)
  • Cloud Code 文档
  • Unity Authentication

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • 其他资源


报告此页面的问题