用户名/密码

SDK 最低版本:2.7.2

请查看以下场景,为您游戏中使用用户名/密码的玩家设置身份验证:

  • 设置用户名/密码。
  • 回归用户登录或创建新用户。
  • 将用户从匿名登录更新为通过用户名/密码帐户进行平台登录。

注意:首先需要初始化 SDK。

设置用户名/密码

  1. 将 Unity Authentication 中的 ID 提供商设置为用户名/密码:
    1. 在 Unity 编辑器菜单中,转到 Edit(编辑)> Project Settings...(项目设置...),然后选择 Services(服务)> Authentication(身份验证)
    2. 将 **ID Providers(ID 提供商)**设置为 Username/Password(用户名/密码),然后选择 Add(添加)
    3. 选择 Save(保存)

注册或回归用户登录

使用 SignUpWithUsernamePasswordAsync 方法来创建使用用户名/密码凭据的新玩家。

注意 1用户名不区分大小写;最少需要包含 3 个字符,最多可以包含 20 个字符,并且仅字母、数字,以及“.”、“-”、“@”或“_”等符号。注意 2密码区分大小写;最少需要包含 8 个字符,最多可以包含 30 个字符,并且至少需要包含 1 个小写字母、1 个大写字母、1 个数字和 1 个符号。

async Task SignUpWithUsernamePasswordAsync(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.SignUpWithUsernamePasswordAsync(username, password);
        Debug.Log("SignUp 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);
    }
}

使用 SignInWithUsernamePasswordAsync 方法来通过用户名/密码凭据登录现有的玩家:

async Task SignInWithUsernamePasswordAsync(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.SignInWithUsernamePasswordAsync(username, password);
        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);
    }
}

将玩家帐户从匿名更新为用户名/密码

在您设置完匿名身份验证后,如果玩家想从匿名帐户升级,创建用户名/密码帐户并登录,那么请在游戏中提示玩家输入凭据。调用 AddUsernamePasswordAsync API 来创建新的用户名/密码帐户并将其添加到玩家名下。

如果 SDK 中存在缓存的玩家,则创建新的用户名/密码并将其添加到缓存的玩家名下。

  1. 通过 SignInAnonymouslyAsync 登录缓存玩家的帐户。
  2. 通过 AddUsernamePasswordAsync 创建新帐户并将其添加到缓存玩家的帐户。

注意:添加到帐户后,用户名/密码帐户便无法移除。

要详细了解缓存的玩家,请参阅缓存玩家登录部分。

async Task AddUsernamePasswordAsync(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.AddUsernamePasswordAsync(username, password);
        Debug.Log("Username and password added.");
    }
    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);
    }
}

更改玩家的密码

向玩家提供用于更改用户名/密码帐户的密码的选项。

用户必须已登录,并且您必须要求用户输入其当前密码新密码,然后调用 UpdatePasswordAsyncUpdatePasswordAsync 会保留用户的身份验证状态,但所有其他已登录设备会要求用户重新登录。

注意:成功调用 UpdatePasswordAsync 时,会使用户从所有已缓存用户的设备上退出登录。

async Task UpdatePasswordAsync(string currentPassword, string newPassword)
{
    try
    {
        await AuthenticationService.Instance.UpdatePasswordAsync(currentPassword, newPassword);
        Debug.Log("Password updated.");
    }
    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);
    }
}