迁移到 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
Remove the highlighted folders in your Unity project.
移除您的 Unity 项目中突出显示的文件夹(点击放大)。

将新插件导入您的 Unity 项目

请按照以下步骤将插件导入您的 Unity 项目:

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

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

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

    3. Window > Google Play Games 下应该有一个新的菜单项。如果没有,请点击 Assets > Refresh 刷新资产,然后再次尝试设置构建平台。

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

  5. Target API level 框中,选择一个版本。

  6. Scripting backend 框中,输入 IL2CPP

  7. Target architectures 框中,选择一个值。

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

    The player settings in your Unity project
    您的 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. 欢迎弹出式窗口已显示。

      Sample welcome popup.
      欢迎弹出式窗口示例(点击放大)。

    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 游戏服务 UI 中不可见。

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

    4. 如果您的游戏使用了以下任一功能,请测试它们以确保它们在迁移后仍然像以前一样工作:

如果您的游戏使用了以下任一功能,请测试它们以确保它们在迁移后仍然像以前一样工作:

  • 排行榜:提交分数并查看排行榜。检查玩家姓名和分数的正确排名和显示。
  • 成就:解锁成就并验证它们是否正确记录并显示在 Play 游戏 UI 中。
  • 已保存的游戏:如果游戏使用已保存的游戏,请确保游戏进度保存和加载功能完美运行。这在跨多个设备和应用更新后进行测试尤为重要。

迁移后任务

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

  1. 使用 Play 应用签名

  2. 创建 AAB 文件

  3. 创建内部测试发布

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