# 使用の準備

> 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 にサインアップしてください。

> **Note:**
>
> 重要: Authentication にサインアップできるのは [組織のオーナー](/cloud/organizations/members-groups-roles.md) のみです。

1. [Unity Dashboard](https://cloud.unity.com/) にサインインします。
2. サイドパネルで **Administration** を選択します。
3. 上部のバナーの **Sign Up** (サインアップ) を選択し、その後の指示に従います。

## プロジェクトのリンク##link-your-project

プロジェクト ID を通じて、Unity エディター内の Unity クラウドプロジェクトにプロジェクトをリンクします。プロジェクト 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 プロジェクト ID を持っています) を選択します。その後、ドロップダウンから組織とプロジェクトを選択し、**Link** (リンク) を選択します。

これで、**Services** (サービス) ウィンドウの **Settings** (設定) タブからプロジェクト 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.");
    };
}
```
