用户名/密码
Provide a username and password option to enable players to authenticate using custom credentials in your game.
阅读时间3 分钟最后更新于 1 个月前
SDK 最低版本:2.7.2
请查看以下场景,为您游戏中使用用户名/密码的玩家设置身份验证:- 设置用户名/密码。
- 回归用户登录或创建新用户。
- 将用户从匿名登录更新为通过用户名/密码帐户进行平台登录。
设置用户名/密码
- 将 Unity Authentication 中的 ID 提供商设置为用户名/密码:
- 在 Unity 编辑器菜单中,转到 Edit(编辑)> Project Settings...(项目设置...),然后选择 Services(服务)> Authentication(身份验证)。
- 将 ID Providers(ID 提供商) 设置为 Username/Password(用户名/密码),然后选择 Add(添加)。
- 选择 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); }}
SignInWithUsernamePasswordAsyncasync 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- 通过 登录缓存玩家的帐户。
SignInAnonymouslyAsync - 通过 创建新帐户并将其添加到缓存玩家的帐户。
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); }}
更改玩家的密码
向玩家提供用于更改用户名/密码帐户的密码的选项。 用户必须已登录,并且您必须要求用户输入其当前密码和新密码,然后调用UpdatePasswordAsyncUpdatePasswordAsyncasync 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); }}