> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Steam と Unity を使用した PlayFab 認証のセットアップ

> AppID セットアップとプレイヤー認証コードをカバーする、PlayFab SDK と Steamworks を使用して Unity ゲームに Steam サインインを追加する手順ガイド。

このチュートリアルでは、**SteamWorks** と Unity を通じて Steam を使用して PlayFab にログインする手順を説明します。

## 前提条件

開始する前に、次のものが必要です:

* PlayFab SDK がインポートされ、タイトル ID が構成された Unity プロジェクト。
* 次の設定を持つ Steam アプリケーション:
  * AppID がすでに設定されている。AppID は [Steam Direct (旧 Greenlight)](https://partner.steamgames.com/steamdirect) プロセスを通じて取得できます。
  * Steam Publisher Web API Key。パブリッシャー キーを生成するには、**Steamworks** ドキュメントの [Creating a Publisher Web API Key](https://partner.steamgames.com/doc/webapi_overview/auth#create_publisher_key) に従ってください。
* [ログインの基本とベスト プラクティス](/services/playfab/identity/player-identity/login/login-basics-best-practices) に精通していること。

## Steam 統合を使用した PlayFab タイトルのセットアップ

Steam 認証のサポートを有効にするには、PlayFab で Steam アドオンを有効にする必要があります。

**Game Manager** ページに移動します:

1. **Add-ons** メニュー項目を選択します。
2. 使用可能な **Add-ons** の一覧で **Steam** を見つけて、タイトル リンクを選択します:

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/game-manager-addons-tab-steam.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=0a73a84c8672d5d3b437e64503483f1d" alt="Game Manager アドオン タブ" width="608" height="681" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/game-manager-addons-tab-steam.png" />

1. **App ID** を入力します。
2. **Web API Key** を入力します。
3. 次に **Install Steam** を選択します。

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/game-manager-install-steam-addon.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=dfdcfff9e84cdabbe596107ad25e3822" alt="Game Manager Steam アドオンのインストール" width="1100" height="935" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/game-manager-install-steam-addon.png" />

## Unity プロジェクトのセットアップ

まず、[Releases ページ](https://github.com/rlabrecque/Steamworks.NET/releases) から Steamworks.NET の最新リリースをダウンロードします。

* リリースの Unity Package 版を取得し、プロジェクトにインポートします。
* パッケージをインポートしたら、Unity を閉じます。
* **プロジェクト** ルート フォルダーに移動します。
* **steam\_appid.txt** ファイルを見つけます。
* ファイルを開き、**App ID** 値を独自のものに置き換えます。

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/unity-project-update-appid.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=080b41d817e52a1728413f63da857637" alt="Unity プロジェクト - AppId の更新" width="391" height="281" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/unity-project-update-appid.png" />

Unity を再度開き、新しいシーンを作成します。

そのシーン内に、**Steam** という新しい **GameObject** を作成します:

1. **SteamManager** コンポーネントを **GameObject** に追加します。このコンポーネントは Steamworks.Net の一部です。
2. **SteamScript** コンポーネントを作成し、**GameObject** に追加します。

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/unity-project-add-steam-object.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=125456b765664eca5ae44b75c48aa9fc" alt="Unity プロジェクト - Steam オブジェクトの追加" width="778" height="385" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/unity-project-add-steam-object.png" />

以下の例は、`SteamScript` コンポーネントのコードを示しています。

```csharp theme={null}
using System.Text;
using PlayFab;
using PlayFab.ClientModels;
using Steamworks;
using UnityEngine;

public class SteamScript : MonoBehaviour
{
    protected Callback<GetTicketForWebApiResponse_t> m_OnGetSteamAuthTicket;

    // Alternatively, you can use this callback if you choose to call SteamUser.GetAuthSessionTicket(...) instead
    // TicketIsServiceSpecific in the PlayFabLoginRequest should be false in this case
    // protected Callback<GetAuthSessionTicketResponse_t> m_OnGetSteamAuthTicketAlternate;

    private HAuthTicket m_hTicket;

    public void Awake()
    {
        m_OnGetSteamAuthTicket = Callback<GetTicketForWebApiResponse_t>.Create(OnGetSteamAuthTicket);
    }
    
    public void OnGUI()
    {
        if (GUILayout.Button("Log In") && SteamManager.Initialized)
        {
            GetSteamAuthTicket();
        }
    }

    private void GetSteamAuthTicket()
    {
        m_hTicket = SteamUser.GetAuthTicketForWebApi("AzurePlayFab");

        if (m_hTicket == HAuthTicket.Invalid)
        {
            Debug.Log("Failed to request steam auth ticket");
        }
        else
        {
            Debug.Log("Steam auth ticket requested");
        }
    }

    private void OnGetSteamAuthTicket(GetTicketForWebApiResponse_t pCallback)
    {
        Debug.Log("Steam auth ticket callback invoked");

        if (pCallback.m_eResult != EResult.k_EResultOK)
        {
            Debug.Log("Failed to get steam auth ticket: " + pCallback.m_eResult);
        }

        StringBuilder sb = new();
        for (int i = 0; i < pCallback.m_cubTicket; ++i)
        {
            sb.AppendFormat("{0:x2}", pCallback.m_rgubTicket[i]);
        }

        PlayFabClientAPI.LoginWithSteam(new LoginWithSteamRequest
        {
            CreateAccount = true,
            SteamTicket = sb.ToString(),
            TicketIsServiceSpecific = true
        }, OnComplete, OnFailed);
    }

    private void OnComplete(LoginResult obj)
    {
        SteamUser.CancelAuthTicket(m_hTicket);
        Debug.Log("Success!");
    }

    private void OnFailed(PlayFabError error)
    {
        SteamUser.CancelAuthTicket(m_hTicket);
        Debug.Log("Failed PlayFab login: " + error.GenerateErrorReport());
    }
}
```

## テスト

エディター内で直接テストできます:

1. シーンを実行し、**Log In** ボタンを選択します。
2. 少し待つと、認証結果として **Success!** を示すコンソール メッセージが表示されるはずです。

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/unity-test-playfab-login-with-steam.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=f5eafa1a09fb3673c292a617c027c69e" alt="Unity Steam を使用した PlayFab ログイン テスト" width="398" height="408" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/unity-test-playfab-login-with-steam.png" />


## Related topics

- [プラットフォーム固有の認証](/ja-jp/services/playfab/identity/player-identity/platform-specific-authentication/index.md)
- [Facebook と Unity を使用した PlayFab 認証のセットアップ](/ja-jp/services/playfab/identity/player-identity/platform-specific-authentication/facebook-unity.md)
- [Kongregate と Unity を使用した PlayFab 認証のセットアップ](/ja-jp/services/playfab/identity/player-identity/platform-specific-authentication/kongregate-unity.md)
- [Google と HTML5 を使用した PlayFab 認証のセットアップ](/ja-jp/services/playfab/identity/player-identity/platform-specific-authentication/google-html5.md)
- [Kongregate と HTML5 を使用した PlayFab 認証のセットアップ](/ja-jp/services/playfab/identity/player-identity/platform-specific-authentication/kongregate-html5.md)
