迁移到 Play 游戏服务 v2 (Unity)

本文档介绍了如何将现有游戏从 games v1 SDK 迁移到 games v2 SDK。适用于 Unity 的 Play 游戏插件(版本 10 及更低版本)使用 games v1 SDK。

准备工作

  • 确保您已设置 Play 管理中心并安装了 Unity Hub。

下载适用于 Unity 的 Google Play 游戏插件

如需获享 Play 游戏服务的最新功能,请下载并安装最新的插件版本。从 GitHub 代码库下载该模块。

移除旧插件

在 Unity Hub 中,移除以下文件夹或文件。

Assets/GooglePlayGames

Assets/GeneratedLocalRepo/GooglePlayGames

Assets/Plugins/Android/GooglePlayGamesManifest.androidlib

Assets/Plugins/Android
从 Unity 项目中移除突出显示的文件夹。
移除 Unity 项目中突出显示的文件夹(点击可放大)。

将新插件导入您的 Unity 项目

如需将该插件导入您的 Unity 项目,请按以下步骤操作:

  1. 打开您的游戏项目。
  2. 在 Unity Hub 中,依次点击 Assets > Import Package > Custom Package,将下载的 unitypackage 文件导入到项目资源中。
  3. 确保当前的 build 平台已设置为 Android

    1. 在主菜单中,依次点击 File > Build Settings

    2. 选择 Android,然后点击 Switch Platform

    3. Window > Google Play Games 下应该会显示新的菜单项。如果未显示,请点击 Assets > Refresh 来刷新资源,然后再次尝试设置 build 平台。

  4. 在 Unity Hub 中,依次点击 File > Build Settings > Player Settings > Other Settings

  5. 目标 API 级别框中,选择一个版本。

  6. Scripting backend 框中,输入 IL2CPP

  7. 目标架构框中,选择一个值。

  8. 记下软件包名称 package_name。您稍后可以使用此信息。

    Unity 项目中的玩家设置
    Unity 项目中的玩家设置。
  9. 从 Play 管理中心复制 Android 资源

  10. 将 Android 资源添加到 Unity 项目

更新自动登录代码

PlayGamesClientConfiguration 初始化类替换为 PlayGamesPlatform.Instance.Authenticate() 类。无需初始化和激活 PlayGamesPlatform。调用 PlayGamesPlatform.Instance.Authenticate() 会提取自动登录的结果。

C#

在 Unity Hub 中,找到包含 PlayGamesClientConfiguration 类的文件。

using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;

public void Start() {
    PlayGamesClientConfiguration config =
        new PlayGamesClientConfiguration.Builder()
    // Enables saving game progress
    .EnableSavedGames()
    // Requests the email address of the player be available
    // will bring up a prompt for consent
    .RequestEmail()
    // Requests a server auth code be generated so it can be passed to an
    // associated backend server application and exchanged for an OAuth token
    .RequestServerAuthCode(false)
    // 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);
    // recommended for debugging:
    PlayGamesPlatform.DebugLogEnabled = true;
    // Activate the Google Play Games platform
    PlayGamesPlatform.Activate();
}

然后更新为以下代码:

using GooglePlayGames;

public void Start() {
    PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication);
}

internal void ProcessAuthentication(SignInStatus status) {
    if (status == SignInStatus.Success) {
        // Continue with Play Games Services
    } else {
        // Disable your integration with Play Games Services or show a login
        // button to ask users to sign-in. Clicking it should call
        // PlayGamesPlatform.Instance.ManuallyAuthenticate(ProcessAuthentication).
    }
}

选择社交平台

如需选择社交平台,请参阅选择社交平台

检索服务器身份验证代码

如需获取服务器端访问代码,请参阅检索服务器身份验证代码

移除退出代码

移除了用于退出的代码。Play 游戏服务不再需要游戏内退出按钮。

移除以下示例中显示的代码:

C#

// sign out
PlayGamesPlatform.Instance.SignOut();

测试游戏

通过测试,确保您的游戏能够按预期运行。您执行的测试取决于游戏的功能。

以下是常见的测试列表。

  1. 登录成功

    1. 自动登录功能会正常运行。用户应在启动游戏时登录 Play 游戏服务。

    2. 系统会显示欢迎弹出式窗口。

      欢迎弹出式窗口示例。
      欢迎弹出式窗口示例(点击可放大)。

    3. 系统会显示成功的日志消息。在终端中运行以下命令:

      adb logcat | grep com.google.android.

      以下示例显示了成功的日志消息:

      [$PlaylogGamesSignInAction$SignInPerformerSource@e1cdecc
      number=1 name=GAMES_SERVICE_BROKER>], returning true for shouldShowWelcomePopup.
      [CONTEXT service_id=1 ]
  2. 确保界面组件一致性

    1. 在各种屏幕尺寸和屏幕方向的 Play 游戏服务界面 (UI) 中,弹出式窗口、排行榜和成就会正确且一致地显示。

    2. Play 游戏服务界面中未显示“退出账号”选项。

    3. 确保您可以成功检索玩家 ID,并且服务器端功能(如果适用)能按预期运行。

    4. 如果游戏使用服务器端身份验证,请彻底测试 requestServerSideAccess 流程。确保服务器收到授权代码,并且可以用该授权代码换取访问令牌。针对网络错误、无效 client ID 场景测试成功和失败场景。

如果您的游戏使用了以下任何功能,请对其进行测试,确保其与迁移前一样正常运行:

  • 排行榜:提交分数和查看排行榜。检查排名是否正确,以及玩家姓名和得分是否正确显示。
  • 成就:解锁成就,并验证成就是否已正确记录并显示在 Play 游戏界面中。
  • 已存档游戏:如果游戏使用已存档游戏,请确保保存和加载游戏进度能够正常运行。这对于跨多部设备进行测试以及在应用更新后进行测试尤为重要。

迁移后的任务

迁移到 games v2 SDK 后,请完成以下步骤。

  1. 使用 Play 应用签名功能

  2. 创建 AAB 文件

  3. 创建内部测试版本

  4. 验证您的应用签名凭据