Google

SDK 最低版本:2.0.0

注意:为了使下面的代码示例正常工作,您必须使用正确的 SDK 和插件版本。

本文将引导您完成以下场景,为您游戏中使用 Google 帐户的玩家设置身份验证:

  • 设置 Google 登录。
  • 回归用户登录或创建新用户。
  • 将用户从匿名登录更新为通过 Google 帐户进行平台登录。

要在您的游戏中为玩家提供 Google 登录选项,请在 Google Play Console 中创建应用,并安装 Google Play Games plugin for Unity v10.14,以登录玩家帐户,获取一次性授权码。这就需要一个开放式身份验证 (ID) 访问令牌,用于 Firebase 或 Google 等其他服务识别玩家身份。

{
    ((PlayGamesLocalUser)Social.localUser).GetIdToken()
}

对于运行 v10.14 和更高版本的插件版本,请参阅 Google Play Games

注意:下方代码示例的前提为,您已经有玩家 Google ID 令牌。

设置 Google 登录

注意:Google 登录由 Google Play Games plugin for Unity v10.14 和更低版本提供支持,该插件使用 Play Games Services v1 SDK。

  1. 请下载并导入 Google Play Games plugin for Unity v10.14。
  2. 设置您的应用以启用 Google 登录。参阅 Google 文档,了解如何为 Google 登录配置您的游戏
  3. 将 Unity Authentication 中的 ID 提供商设置为 Google:
    1. 在 Unity 编辑器菜单中,访问 Edit(编辑)> Project Settings…(项目设置…),然后从导航菜单中选择 Services(服务)> Authentication(身份验证)
    2. 将 **ID Providers(ID 提供商)**设置为 Google,然后选择 Add(添加)
    3. 在 **Client ID(客户端 ID)**文本字段中输入 Web App client ID(Web 应用客户端 ID),然后选择 Save(保存)

注意:您必须在插件设置对话框中设置 **Web App client ID(Web 应用客户端 ID)**才能使用 Authentication SDK。

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 API,将玩家帐户与 Google ID 令牌关联。

如果 SDK 上有缓存的玩家帐户,您可以将缓存的玩家帐户与 Google 帐户关联。

  1. 通过 SignInAnonymouslyAsync 登录缓存玩家的帐户。
  2. 通过 LinkWithGoogleAsync 将缓存的玩家帐户与 Google 帐户关联。

要详细了解缓存的玩家,请参阅缓存玩家登录部分。

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 帐户关联

使用 UnlinkGoogleAsync API 可以取消玩家帐户与其 Google 帐户的关联。一旦取消关联,如果玩家帐户不再关联到任何其他身份,则会转换为匿名帐户。

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