# 开始使用

> Follow this workflow to create a Unity account, link your project, and install the SDK to get started with Unity Authentication.

为了使用 Authentication，您需要：

1. 注册 UGS。
2. 将后台关联到 Unity 编辑器项目。
3. 在您的游戏代码中安装 SDK。
4. 初始化 SDK。

## 注册##sign-up

如果您还没有 Unity 帐户，[创建一个帐户](/services.md)并[创建一个新项目](/services/getting-started.md)，以注册使用 Unity Gaming Services（Unity 游戏服务）。

> **Note:**
>
> 重要：只有[组织所有者](/cloud/organizations/members-groups-roles.md)才能注册使用 Authentication。

1. 登录 [Unity Dashboard](https://cloud.unity.com/)。
2. 在侧面板中选择 **Administration（管理）**。
3. 选择顶部横幅中的 **Sign Up（注册）** 并按照说明操作。

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

在 Unity 编辑器中通过 Project ID 将您的项目关联到 Unity Cloud 项目。通过以下步骤获取 Project ID。

1. 在 Unity 编辑器菜单中，选择 **Edit（编辑）**> **Project Settings（项目设置）**> **Services（服务）** 选项卡。


   **Frame:**
   ![](/api/media?file=/authentication/media/images/edit-button.png)
2. 如果您尚未登录您的 Unity ID，请创建新的 Unity ID 或者登录。
3. 要创建新的项目，请选择您的组织，然后选择 **Create（创建）**。要关联到现有的项目，请选择 **I already have a Unity Project ID（我已经有 Unity Project ID）**。接下来，从下拉选单中选择您的组织和项目，然后选择 **Link（关联）**。

现在，您可以在 **Services（服务）** 窗口的 **Settings（设置）** 选项卡中找到 Project ID。

## 安装 SDK##install-the-sdk

为 Unity 安装全新的 Authentication 软件包：

1. 在 Unity 编辑器中，打开 **Window（窗口）**> **Package Manager（包管理器）**。
2. 在 Package Manager（包管理器）中，选择 **Unity Registry（Unity 注册表）** 列表视图。
3. 搜索 **Authentication**，或者在软件包列表中查找。
4. 选择该包，然后选择 **Install（安装）**。

请参阅 [Package Manager（包管理器）](https://docs.unity3d.com/Manual/upm-ui.html)文档，了解更多信息。

## 初始化 Unity Services SDK##initialize-the-unity-services-sdk

要在您的游戏中实现 Unity Authentication，请按照以下代码片段[初始化项目中包含的所有 Unity Services SDK](/services/getting-started.md)：

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

public class InitializationExample : MonoBehaviour
{
	async void Awake()
	{
		try
		{
			await UnityServices.InitializeAsync();
		}
		catch (Exception e)
		{
			Debug.LogException(e);
		}
	}
}
```

### 注册身份验证事件##register-authentication-events

要接收有关您玩家状态的更新信息，请将功能注册到 `SignedIn`、`SignInFailed` 和 `SignedOut` 事件处理程序。

```cs
// Setup authentication event handlers if desired
void SetupEvents() {
  AuthenticationService.Instance.SignedIn += () => {
    // Shows how to get a playerID
    Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");

    // Shows how to get an access token
    Debug.Log($"Access Token: {AuthenticationService.Instance.AccessToken}");

  };

  AuthenticationService.Instance.SignInFailed += (err) => {
    Debug.LogError(err);
  };

  AuthenticationService.Instance.SignedOut += () => {
    Debug.Log("Player signed out.");
  };

  AuthenticationService.Instance.Expired += () =>
    {
        Debug.Log("Player session could not be refreshed and expired.");
    };
}
```
