Steam
Provide a Steam sign-in option to enable players to authenticate using their Steam accounts in your game.
阅读时间3 分钟最后更新于 1 个月前
SDK 最低版本:2.0.0
本文将引导您完成以下场景,为您游戏中使用 Steam 帐户的玩家设置身份验证:- 设置 Steam 帐户登录。
- 回归用户登录或创建新用户。
- 将用户从匿名登录更新为通过 Steam 帐户进行平台登录。
设置 Steam 帐户登录
- 根据 Steamworks 文档创建应用并记下应用 ID。
- 根据使用 Web API 密钥进行身份验证文档创建发布者 Web API 密钥。
-
根据 Unity 专用 SDK 安装说明安装 SteamWorks.Net SDK。
- 将 SteamWorks.Net SDK 复制到您项目的 Assets/ 文件夹。
- 打开 Unity 项目根目录中的 steam_appid.txt 并将 480 更改为您自己的应用 ID。
- 将 Steam Manager 游戏组件添加到该场景中的游戏对象。这样会初始化 Steam 库。
- 测试前,您要确保已安装并登录 Steam。Steam 用户能在库中找到您正在开发的游戏。
-
将 Unity Authentication 中的 ID 提供商设置为 Steam:
- 在 Unity 编辑器菜单中,访问 Edit(编辑)> Project Settings…(项目设置…),然后从导航菜单中选择 Services(服务)> Authentication(身份验证)。
- 将 ID Providers(ID 提供商) 设置为 Steam,然后选择 Add(添加)。
- 在 App ID(应用 ID) 文本字段中输入应用 ID。
- 在 Key(密钥) 文本字段中输入发布者 Web API 密钥,然后选择 Save(保存)。
- 使用以下样本代码执行 Steam 登录。请参阅 GetAuthTicketForWebApi 文档。Unity Authentication SDK 仅接受 Steam 会话票证,不接受加密的应用程序票证。
Callback<GetTicketForWebApiResponse_t> m_AuthTicketForWebApiResponseCallback;string m_SessionTicket;string identity = "unityauthenticationservice"void SignInWithSteam(){ // It's not necessary to add event handlers if they are // already hooked up. // Callback.Create return value must be assigned to a // member variable to prevent the GC from cleaning it up. // Create the callback to receive events when the session ticket // is ready to use in the web API. // See GetAuthSessionTicket document for details. m_AuthTicketForWebApiResponseCallback = Callback<GetTicketForWebApiResponse_t>.Create(OnAuthCallback); SteamUser.GetAuthTicketForWebApi(identity);}void OnAuthCallback(GetTicketForWebApiResponse_t callback){ m_SessionTicket = BitConverter.ToString(callback.m_rgubTicket).Replace("-", string.Empty); m_AuthTicketForWebApiResponseCallback.Dispose(); m_AuthTicketForWebApiResponseCallback = null; Debug.Log("Steam Login success. Session Ticket: " + m_SessionTicket); // Call Unity Authentication SDK to sign in or link with Steam, displayed in the following examples, using the same identity string and the m_SessionTicket.}
回归玩家登录或创建新玩家
使用SignInWithSteamAsync- 通过 Steam 凭据创建新的 Unity Authentication 玩家。
- 通过 Steam 凭据登录现有玩家。
SignInWithSteamAsyncSignInWithSteamAsyncSignInWithSteamAsyncasync Task SignInWithSteamAsync(string ticket, string identity){ try { await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity); Debug.Log("SignIn is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
将玩家从匿名更新为 Steam 帐户
在设置完匿名身份验证后,如果玩家想从匿名升级为 Steam 帐户,并使用 Steam 帐户登录,则游戏应提示玩家触发 Steam 登录并从 Steam 获取会话票证。然后,调用 LinkWithSteamAsync API,将玩家关联到 Steam 会话票证。 如果 SDK 上有缓存的玩家帐户,您可以将缓存的玩家帐户与 Steam 帐户关联。- 通过 登录缓存玩家的帐户。
SignInAnonymouslyAsync - 通过 将缓存的玩家帐户与 Steam 帐户关联。
LinkWithSteamAsync
如果玩家通过登录或创建新玩家配置文件而触发 Steam 登录,并且您已使用相同的 identity 参数收到 Steam 访问令牌,请调用以下 API 来对玩家进行身份验证。async Task LinkWithSteamAsync(string ticket, string identity){ try { await AuthenticationService.Instance.LinkWithSteamAsync(ticket, identity); Debug.Log("Link is successful."); } catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked) { // Prompt the player with an error message. Debug.LogError("This user is already linked with another account. Log in instead."); } catch (Exception ex) { Debug.LogError("Link failed."); Debug.LogException(ex); }}
async Task SignInWithSteamAsync(string ticket, string identity){ try { await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
回归玩家登录或创建新玩家来登录附加 App ID
使用相同的SignInWithSteamAsyncasync Task SignInWithSteamAsync(string ticket, string identity, string appId){ try { await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity, appId); Debug.Log("SignIn is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
实现 Steam 登录身份验证
您可以使用 Steam 会话票证进行登录或关联 Steam 帐户。调用以下 API 登录玩家:SignInWithSteamAsync(string ticket, string identity)Unlink Steam account#Use the `UnlinkSteamAsync` API so your players can unlink their Steam account. Once unlinked, if their account isn’t linked to any additional identity, it transitions to an anonymous account.async Task UnlinkSteamAsync(string idToken){ try { await AuthenticationService.Instance.UnlinkSteamAsync(idToken); Debug.Log("Unlink is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}