# 开始使用 Leaderboards

> Set up and integrate Leaderboards into your project using the Unity Dashboard, Software Development Kit, CLI, or REST API.

使用 Leaderboards 的第一步是了解如何实施该功能。本节介绍了如何针对您的项目设置 Leaderboards。

| **主题**                                                          | **描述**                                                         |
| --------------------------------------------------------------- | -------------------------------------------------------------- |
| [在 Unity Dashboard（Unity 后台）中使用 Leaderboards](#unity-dashboard) | 了解 Leaderboards 在 Unity Dashboard（Unity 后台）中的位置。               |
| [SDK 安装和设置](#unity-sdk-installation-and-setup)                  | 了解如何将您的项目与 Unity Gaming Services（Unity 游戏服务）项目关联以及如何安装和设置 SDK。 |
| [在 UGS CLI 中使用 Leaderboards](#leaderboards-in-the-ugs-cli)      | 了解如何使用 Unity CLI 与 Leaderboards 交互。                            |
| [使用 Leaderboards REST API](#leaderboards-rest-api)              | 了解在不使用 Unity SDK 的情况下可用于访问 Leaderboards 的 REST API。            |
| [搭配 Cloud Code 使用 Leaderboards](./access-cloud-code.md)         | 了解如何使用 Cloud Code 服务访问 Leaderboards。                           |

## Unity Dashboard（Unity 后台）##unity-dashboard

您可以在 [Unity Dashboard（Unity 后台）](https://dashboard.unity3d.com/gaming/organizations/default/projects/default/environments/default/leaderboards/about)中访问 Leaderboards。在主菜单中选择 **LiveOps** 后，即可找到 Leaderboards。

Dashboard（后台）可用于创建和管理排行榜并查看其条目。

## Unity SDK 安装和设置##unity-sdk-installation-and-setup

Leaderboards SDK 需要 Unity 2020.3.0 或更高版本。

### 安装 Leaderboards SDK##install-leaderboards-sdk

可以通过 Unity 编辑器安装软件包。

导航到 **Window（窗口）> Package Manager（包管理器）**，然后在左上方的 Packages（包）下拉选单中选择 **Unity Registry（Unity 注册表）**。您可以执行以下任一操作：

* 在右上方的搜索栏中搜索 **Leaderboards**。
* 按名称添加包。打开 **+** 所示的下拉选单，然后选择 **“Add package by name...（按名称添加包...）”**。搜索 SDK 包 ID **“com.unity.services.leaderboards”**，不包含其版本。


**Frame:**
![](/api/media?file=/leaderboards/media/images/install-package-by-name.png)

> **Note:**
>
> **注意**：`Samples` 部分可供您向项目中导入示例代码，从而帮助您在从游戏中调用 Leaderboards SDK。

安装完成后，可在 Unity 脚本中通过引用 `Unity.Services.Leaderboards` 命名空间使用 Leaderboards SDK。

C#:

```cs
using Unity.Services.Leaderboards;
```

### 安装 Authentication SDK##install-authentication-sdk

Leaderboards 包依赖于 Authentication 包。您可以通过[使用匿名登录](/authentication/use-anon-sign-in.md)或[特定平台身份验证](/authentication/approaches-to-authentication.md)，使得 [Unity Authentication](/authentication/.md) 服务创建一个帐户来保存玩家分数。

安装 Leaderboards 包时，Authentication 作为依赖项进行安装。有关手动安装包的信息，请参阅[安装来自注册表的包](https://docs.unity3d.com/Manual/upm-ui-install.html)。

安装完成后，可在 Unity 脚本中通过引用 `Unity.Services.Authentication` 命名空间使用 Authentication SDK。

C#:

```cs
using Unity.Services.Authentication;
```

### 关联您的 Unity 项目##link-your-unity-project

安装完 Authentication 包后，系统会提示您将 Unity 项目与 Unity Game Services Project ID 关联，如下所示。按屏幕上提示中的说明来关联您的项目。


**Frame:**
![](/api/media?file=/leaderboards/media/images/link-unity-project.png)

如果未看到提示，请执行以下步骤，以便将您的 Unity 项目与 Project ID 关联：

* 在 Unity 编辑器中，选择 **Edit（编辑）**> **Project Settings...（项目设置...）**.
* 选择 **Services（服务）**，
  * 如果您有 Unity ID，请登录。
  * 否则，选择 **Create a Unity ID（创建 Unity ID）**。
* 选择您的项目。
* 选择 **Link（关联）**。

### 初始化 SDK 并对玩家进行身份验证##initialize-the-sdks-and-authenticate-the-player

在使用 Leaderboards SDK 及其依赖项之前，必须先从 Unity 脚本生命周期回调中对它们进行初始化。以下示例使用 `Awake` 回调。实现方法是通过调用 `await UnityServices.InitializeAsync();` 来初始化所有通过 Core SDK（引用 `Unity.Services.Core` 命名空间）安装的服务。

SDK 初始化完成后，会对玩家进行身份验证。以下示例使用匿名身份验证来为玩家创建匿名帐户，以便保存其分数。有关其他可用的身份验证方法，请参阅 [Unity Authentication](/authentication/.md) 文档。

C#:

```cs
using UnityEngine;
using Unity.Services.Core;
using Unity.Services.Authentication;

public class LeaderboardsSample : MonoBehaviour
{
  private async void Awake()
    {
        await UnityServices.InitializeAsync();
        await AuthenticationService.Instance.SignInAnonymouslyAsync();
    }
}
```

### 后续步骤##next-steps

完成上述步骤后，即可从 `Unity.Services.Leaderboards` 命名空间使用 Leaderboards SDK。请参阅[功能](./features.md)、SDK 文档和 SDK 示例，了解有关 Leaderboards 功能集及其使用方法的更多信息。

## 在 UGS CLI 中使用 Leaderboards##leaderboards-in-the-ugs-cli

Unity Gaming Services（Unity 游戏服务）(UGS) 命令行界面为 Unity Dashboard（Unity 后台）提供了一种可扩展且可自动化的替代方案，可以改进团队的工作流程和提高其效率。可使用 CLI 来管理、测试和部署 Leaderboards 配置。

请参阅有关如何安装和使用 CLI 的[文档](https://services.docs.unity.com/guides/ugs-cli/latest/general/overview)。

## Leaderboards REST API##leaderboards-rest-api

不使用 Unity 的开发者可以通过 Web 终端或 REST API 访问 API。REST API 提供更大的灵活性，让您可以利用喜欢的语言和游戏开发引擎来自动执行工作流程。

Leaderboards 服务提供以下 REST API：

* [Leaderboards Player API](https://services.docs.unity.com/leaderboards/)，用于执行玩家操作（例如，提交分数或获取分数）。
* [Leaderboards Admin API](https://services.docs.unity.com/leaderboards-admin/)，用于执行管理员操作（例如创建或更新排行榜、删除或重置分数）以及代表玩家执行玩家操作。

## 其他资源##leaderboards-authoring

* [Unity Dashboard（Unity 后台）](https://dashboard.unity3d.com/)
* [Package Manager（包管理器）](https://docs.unity3d.com/Manual/upm-ui.html)
* [Unity Authentication](/authentication/.md)
