文档

​
​

Development

User Acquisition

Monetization

工业

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
  • Reference
  • Privacy and consent
  1. Leaderboards

Unity SDK sample

Refer to sample code that implements Leaderboards functionality in a Unity project.
阅读时间2 分钟
最后更新于 9 个月前

This sample (with additional methods) is found as part of the Leaderboards SDK package by checking the box to include
Samples
when installing from the package manager.
The following example presumes the existence of a leaderboard with the ID
my-first-leaderboard
. You can create this from the Unity Dashboard.
The sample then initializes the Unity services and uses anonymous authentication to sign-in the player in the
Awake
lifecycle callback (see SDK Installation & Setup) and then uses the SDK to provide methods for interacting with the leaderboard data and printing it to the console.
using System.Collections.Generic;using System.Threading.Tasks; using Newtonsoft.Json;using Unity.Services.Authentication;using Unity.Services.Core;using Unity.Services.Leaderboards;using UnityEngine;public class LeaderboardsSample : MonoBehaviour{ // Create a leaderboard with this ID in the Unity Dashboard const string LeaderboardId = "my-first-leaderboard"; string VersionId { get; set; } int Offset { get; set; } int Limit { get; set; } int RangeLimit { get; set; } List<string> FriendIds { get; set; } async void Awake() { await UnityServices.InitializeAsync(); await SignInAnonymously(); } async Task SignInAnonymously() { AuthenticationService.Instance.SignedIn += () => { Debug.Log("Signed in as: " + AuthenticationService.Instance.PlayerId); }; AuthenticationService.Instance.SignInFailed += s => { // Take some action here... Debug.Log(s); }; await AuthenticationService.Instance.SignInAnonymouslyAsync(); } public async void AddScore() { var scoreResponse = await LeaderboardsService.Instance.AddPlayerScoreAsync(LeaderboardId, 102); Debug.Log(JsonConvert.SerializeObject(scoreResponse)); } public async void GetScores() { var scoresResponse = await LeaderboardsService.Instance.GetScoresAsync(LeaderboardId); Debug.Log(JsonConvert.SerializeObject(scoresResponse)); } public async void GetPaginatedScores() { Offset = 10; Limit = 10; var scoresResponse = await LeaderboardsService.Instance.GetScoresAsync(LeaderboardId, new GetScoresOptions{Offset = Offset, Limit = Limit}); Debug.Log(JsonConvert.SerializeObject(scoresResponse)); } public async void GetPlayerScore() { var scoreResponse = await LeaderboardsService.Instance.GetPlayerScoreAsync(LeaderboardId); Debug.Log(JsonConvert.SerializeObject(scoreResponse)); } public async void GetVersionScores() { var versionScoresResponse = await LeaderboardsService.Instance.GetVersionScoresAsync(LeaderboardId, VersionId); Debug.Log(JsonConvert.SerializeObject(versionScoresResponse)); }}

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

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

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


    报告此页面的问题