> ## 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 驗證

> 逐步教學課程，說明如何使用 PlayFab SDK 和 Steamworks 將 Steam 登入新增至 Unity 遊戲，涵蓋 AppID 設定和玩家驗證程式碼。

本教學課程將引導您完成透過 **SteamWorks** 和 Unity 使用 Steam 登入 PlayFab 的步驟。

## 必要條件

開始之前，您應該具備：

* 已匯入 PlayFab SDK 並設定遊戲識別碼的 Unity 專案。
* 具備下列項目的 Steam 應用程式：
  * 已設定的 AppID。您可以透過 [Steam Direct (先前稱為 Greenlight)](https://partner.steamgames.com/steamdirect) 程序取得 AppID。
  * Steam 發行者 Web API 金鑰。若要產生發行者金鑰，請依照 **Steamworks** 文件中的 [Creating a Publisher Web API Key](https://partner.steamgames.com/doc/webapi_overview/auth#create_publisher_key) 操作。
* 熟悉[登入基本概念和最佳做法](/zh-TW/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 Add-ons 索引標籤" 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。
* 瀏覽至 **Project** 根資料夾。
* 找到 **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

- [平台特定驗證](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/index.md)
- [使用 Facebook 和 Unity 設定 PlayFab 驗證](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/facebook-unity.md)
- [使用 Kongregate 和 Unity 設定 PlayFab 驗證](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/kongregate-unity.md)
- [使用 Google 和 HTML5 設定 PlayFab 驗證](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/google-html5.md)
- [使用 Kongregate 和 HTML5 設定 PlayFab 驗證](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/kongregate-html5.md)
