文档

支持

Authentication

Open Unity Dashboard

Authentication

用户名/密码

Provide a username and password option to enable players to authenticate using custom credentials in your game.
阅读时间3 分钟最后更新于 1 个月前

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
方法来创建使用用户名/密码凭据的新玩家。
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); }}

更改玩家的密码

向玩家提供用于更改用户名/密码帐户的密码的选项。 用户必须已登录,并且您必须要求用户输入其当前密码新密码,然后调用
UpdatePasswordAsync
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); }}