Google Play Games
Provide a Google Play Games sign-in option to provide a frictionless authentication experience for Android players.
阅读时间2 分钟最后更新于 1 个月前
SDK 最低版本:2.1.0
本文将引导您完成以下场景,为您游戏中使用 Google Play Games 帐户的玩家设置身份验证: 注意:下方代码示例的前提为,您已经有玩家一次性授权码。- 设置 Google Play Games 登录。
- 回归用户登录或创建新用户。
- 将用户从匿名登录更新为通过 Google Play Games 帐户进行平台登录。
设置 Google Play Games 登录
注意:Google Play Games 登录仅与 Android 设备兼容。 注意:Google Play Games 登录由 Google Play Games plugin for Unity v11.01 和更高版本提供支持,建议您使用 Play Games Services v2 SDK。- 下载并导入 Google Play Games Plugin for Unity。
- 设置您的应用以启用 Google 登录。参阅 Google 文档,了解如何为 Google 登录配置您的游戏。
-
将 Unity Authentication 中的 ID 提供商设置为 Google:
- 在 Unity 编辑器菜单中,访问 Edit(编辑)> Project Settings…(项目设置…),然后从导航菜单中选择 Services(服务)> Authentication(身份验证)。
- 将 ID Providers(ID 提供商) 设置为 Google Play Services(Google Play 服务),然后单击 Add(添加)。
- 在 Client ID(客户端 ID) 文本字段中输入 Web App client ID(Web 应用客户端 ID)。
-
在 Client Secret(客户端密钥) 文本字段中,输入 Web App client secret(Web 应用客户端密钥),然后选择 Save(保存)。
using GooglePlayGames;using GooglePlayGames.BasicApi;using UnityEngine;public class GooglePlayGamesExampleScript : MonoBehaviour{ public string Token; public string Error; void Awake() { //Initialize PlayGamesPlatform PlayGamesPlatform.Activate(); LoginGooglePlayGames(); } public void LoginGooglePlayGames() { PlayGamesPlatform.Instance.Authenticate((success) => { if (success == SignInStatus.Success) { Debug.Log("Login with Google Play games successful."); PlayGamesPlatform.Instance.RequestServerSideAccess(true, code => { Debug.Log("Authorization code: " + code); Token = code;// This token serves as an example to be used for SignInWithGooglePlayGames }); } else { Error = "Failed to retrieve Google play games authorization code"; Debug.Log("Login Unsuccessful"); } }); }}
回归玩家登录或创建新玩家
您可以使用SignInWithGooglePlayGamesAsync- 通过 Google Play Games 凭据创建新的 Unity Authentication 玩家帐户。
- 通过 Google Play Games 凭据登录现有玩家帐户。
SignInWithGooglePlayGamesAsyncSignInWithGooglePlayGamesAsyncSignInWithGooglePlayGamesAsyncasync Task SignInWithGooglePlayGamesAsync(string authCode){ try { await AuthenticationService.Instance.SignInWithGooglePlayGamesAsync(authCode); 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 Play Games 帐户
在您设置完匿名身份验证后,如果玩家想从匿名升级为 Google Play Games 帐户并登录,您应在游戏中引导玩家触发 Google Play Games 登录并从 Google 获取一次性授权码。然后,调用LinkWithGooglePlayGamesAsync- 通过 登录缓存玩家的帐户。
SignInAnonymouslyAsync - 通过 将缓存的玩家帐户与 Google Play Games 帐户关联。
LinkWithGooglePlayGamesAsync
async Task LinkWithGooglePlayGamesAsync(string authCode){ try { await AuthenticationService.Instance.LinkWithGooglePlayGamesAsync(authCode); 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 Play 帐户关联
使用UnlinkGooglePlayGamesAsyncasync Task UnlinkGooglePlayGamesAsync(string idToken){ try { await AuthenticationService.Instance.UnlinkGooglePlayGamesAsync(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); }}