文档

​
​

Development

User Acquisition

Monetization

工业

Authentication

Open Unity Dashboard

Authentication

LiveOps
​
​
Authentication
  • Overview
  • Get started
  • Concepts
  • Tutorials
    • Anonymous sign-in
    • Google Play Games
    • Apple Game Center
    • Steam
    • Facebook
    • Unity Player Accounts
    • Oculus
    • Username and Password
    • Code-Link
    • Google
    • Apple
    • Custom OpenID Connect
    • Custom ID
  • Reference
  • Privacy and consent
  1. Unity Authentication

用户名/密码

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

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); }}

更改玩家的密码

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

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • SDK 最低版本:2.7.2

    • 设置用户名/密码

    • 注册或回归用户登录

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

    • 更改玩家的密码


报告此页面的问题