文档

支持

Vivox Unity SDK

Vivox Unity SDK

使用自定义 ID 登录

Sign in users to Vivox with custom identifiers.
阅读时间3 分钟最后更新于 13 天前

如果您已经拥有创建玩家 ID 的现有玩家管理系统,则可以使用自定义 ID 提供商来关联身份。 先决条件
  1. 设置 Unity Cloud 项目
    • 确保您拥有 Project ID、环境 ID 和环境名称。
  2. 启用 UAS 自定义 ID 提供商确保您在此设置中拥有服务帐户凭据。
此设置的总体流程如下:
登录
  1. 在登录时向 UAS 自定义 ID 提供商注册所有用户,并为玩家获取
    accessToken
    sessionTokens
  2. 使用
    accessToken
    sessionToken
    在客户端设置 AuthenticationService。

客户端设置

除了以下进一步介绍的服务器端登录设置外,还需要从后端获取访问令牌并使用
AuthenticationService
进行处理:
ProcessAuthenticationTokens
用于处理 Unity Authentication 服务访问令牌,并使这些令牌可供游戏中集成的其他需要玩家进行身份验证的 UGS SDK 使用。由于访问令牌会到期,因此必须手动刷新访问令牌并使用新的访问令牌调用
ProcessAuthenticationTokens
。您可以使用访问令牌和会话令牌来调用
ProcessAuthenticationTokens
。Unity Authentication SDK 会在访问令牌到期之前刷新令牌,保持玩家会话处于活动状态,并允许 登录缓存的玩家。
using Unity.Services.Authentication;using Unity.Services.Vivox;void SignUserWithCustomTokenWithAutoRefresh(){ try { // Check if a cached player already exists by checking if the session token exists if (AuthenticationService.Instance.SessionTokenExists) { // This call will sign in the cached player. await AuthenticationService.Instance.SignInAnonymouslyAsync(); Debug.Log("Cached user sign in succeeded!"); } else { var userTokens = // Fetch the user tokens using your method calls AuthenticationService.Instance.ProcessAuthenticationTokens(userTokens.AccessToken, userTokens.SessionToken) } // Shows how to get the playerID Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}"); await VivoxService.Instance.InitializeAsync(); } 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); } catch (Exception ex) { // Handle exceptions from your method call Debug.LogException(ex); }}
请注意代码样本仅描述了一种方法,而不代表一类方法。 需要从后端获取访问令牌。如需了解更多与此相关的信息,请参阅下面的服务器端设置

服务器端设置

在玩家登录期间,确保每个玩家都已向 UAS 自定义 ID 提供商注册,并为玩家获取
accessToken
sessionToken
。如果您尚未执行此操作,请参阅启用 UAS 自定义 ID 提供商以进行此设置。通过使用服务帐户凭据,您可以发送请求以在游戏服务器上注册玩家:
  1. 调用 Token Exchange API 以获取无状态令牌。
  2. 调用使用自定义 ID 登录 API
curl --location 'https://player-auth.services.api.unity.com/v1/projects/<PROJECT_ID>/authentication/server/custom-id' \--header 'Content-Type: application/json' \--header 'UnityEnvironment: <ENVIRONMENT_NAME>' \--header 'Authorization: Bearer <STATELESS_TOKEN' \--data '{ "externalId": "YOUR_CUSTOM_PLAYER_ID"}'
此请求的响应将包含一个
idToken
和一个
sessionToken
,您必须将它们返回给客户端以初始化
AuthenticationService
。在此上下文中,
idToken
可用作
accessToken
或者,可以使用 Player Names API 为玩家设置名称。如果您正在使用 Vivox Safety 产品,这将非常有用,因为它允许在 Moderation 后台上向仲裁者显示玩家名称。如果玩家更改了名称,请务必保持玩家名称为最新名称。您还可以通过玩家名称管理直接从客户端管理玩家名称。