> ## 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 和已配置 title ID 的 Unity 项目。
* 一个 Steam 应用程序：
  * AppID 已设置。可以通过 [Steam Direct（原 Greenlight）](https://partner.steamgames.com/steamdirect)流程获取 AppID。
  * Steam Publisher Web API Key。要生成 publisher key，请按照 **Steamworks** 文档中的[创建 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 title

要启用对 Steam 授权的支持，PlayFab 要求你启用 Steam 附加组件。

转到你的 **Game Manager** 页面：

1. 选择 **Add-ons** 菜单项。
2. 在可用 **Add-ons** 列表中，找到 **Steam** 并选择 title 链接：

<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 tab" 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 install Steam Add-on" width="1100" height="935" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/steam-unity/game-manager-install-steam-addon.png" />

## 设置 Unity 项目

首先从[发布页面](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 project - Update 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 project - Add Steam object" 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 test PlayFab login with Steam" 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-CN/services/playfab/identity/player-identity/platform-specific-authentication/index.md)
- [使用 Facebook 和 Unity 设置 PlayFab 身份验证](/zh-CN/services/playfab/identity/player-identity/platform-specific-authentication/facebook-unity.md)
- [使用 Kongregate 和 Unity 设置 PlayFab 身份验证](/zh-CN/services/playfab/identity/player-identity/platform-specific-authentication/kongregate-unity.md)
- [使用 Google 和 HTML5 设置 PlayFab 身份验证](/zh-CN/services/playfab/identity/player-identity/platform-specific-authentication/google-html5.md)
- [使用 Facebook 和 HTML5 设置 PlayFab 身份验证](/zh-CN/services/playfab/identity/player-identity/platform-specific-authentication/facebook-html5.md)
