Provide a Google sign-in option to enable players to authenticate using their Google accounts in your game.
阅读时间3 分钟最后更新于 1 个月前
SDK 最低版本:2.0.0
本文将引导您完成以下场景,为您游戏中使用 Google 帐户的玩家设置身份验证:
- 设置 Google 登录。
- 回归用户登录或创建新用户。
- 将用户从匿名登录更新为通过 Google 帐户进行平台登录。
对于运行 v10.14 和更高版本的插件版本,请参阅 Google Play Games。{ ((PlayGamesLocalUser)Social.localUser).GetIdToken()}
设置 Google 登录
注意:Google 登录由 Google Play Games plugin for Unity v10.14 和更低版本提供支持,该插件使用 Play Games Services v1 SDK。- 请下载并导入 Google Play Games plugin for Unity v10.14。
- 设置您的应用以启用 Google 登录。参阅 Google 文档,了解如何为 Google 登录配置您的游戏。
- 将 Unity Authentication 中的 ID 提供商设置为 Google:
- 在 Unity 编辑器菜单中,访问 Edit(编辑)> Project Settings…(项目设置…),然后从导航菜单中选择 Services(服务)> Authentication(身份验证)。
- 将 ID Providers(ID 提供商) 设置为 Google,然后选择 Add(添加)。
- 在 Client ID(客户端 ID) 文本字段中输入 Web App client ID(Web 应用客户端 ID),然后选择 Save(保存)。
void InitializePlayGamesLogin(){ var config = new PlayGamesClientConfiguration.Builder() // Requests an ID token be generated. // This OAuth token can be used to // identify the player to other services such as Firebase. .RequestIdToken() .Build(); PlayGamesPlatform.InitializeInstance(config); PlayGamesPlatform.DebugLogEnabled = true; PlayGamesPlatform.Activate();}void LoginGoogle(){ Social.localUser.Authenticate(OnGoogleLogin);}void OnGoogleLogin(bool success){ if (success) { // Call Unity Authentication SDK to sign in or link with Google. Debug.Log("Login with Google done. IdToken: " + ((PlayGamesLocalUser)Social.localUser).GetIdToken()); } else { Debug.Log("Unsuccessful login"); }}
回归玩家登录或创建新玩家
使用SignInWithGoogleAsync- 通过 Google 凭据创建新的 Unity Authentication 玩家帐户。
- 通过 Google 凭据登录现有玩家帐户。
async Task SignInWithGoogleAsync(string idToken){ try { await AuthenticationService.Instance.SignInWithGoogleAsync(idToken); 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); }}
将玩家帐户从匿名更新为 Google 帐户
在您设置完匿名身份验证后,如果玩家想从匿名升级为 Google 帐户并登录,您应在游戏中引导玩家触发 Google 登录并从 Google 获取 ID 令牌。然后,调用LinkWithGoogleAsync- 通过 登录缓存玩家的帐户。
SignInAnonymouslyAsync - 通过 将缓存的玩家帐户与 Google 帐户关联。
LinkWithGoogleAsync
async Task LinkWithGoogleAsync(string idToken){ try { await AuthenticationService.Instance.LinkWithGoogleAsync(idToken); Debug.Log("Link is successful."); } catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked) { // Prompt the player with an error message. Debug.LogError("This user is already linked with another account. Log in instead."); } 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); }}
取消 Google 帐户关联
使用UnlinkGoogleAsyncasync Task UnlinkGoogleAsync(string idToken){ try { await AuthenticationService.Instance.UnlinkGoogleAsync(idToken); Debug.Log("Unlink 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); }}