本文档介绍了如何将现有游戏从 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 Hub 中,依次点击 Assets > Import Package > Custom Package,将下载的
unitypackage
文件导入您项目的资产中。 确保您的当前构建平台设置为 Android。
在主菜单中,点击 File > Build Settings。
选择 Android,然后点击 Switch Platform。
在 Window > Google Play Games 下应该有一个新的菜单项。如果没有,请点击 Assets > Refresh 刷新资产,然后再次尝试设置构建平台。
在 Unity Hub 中,依次点击 File > Build Settings > Player Settings > Other Settings。
在 Target API level 框中,选择一个版本。
在 Scripting backend 框中,输入
IL2CPP
。在 Target architectures 框中,选择一个值。
记下软件包名称 package_name。您以后可以使用此信息。
您的 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();
测试您的游戏
通过测试确保您的游戏按设计运行。您执行的测试取决于游戏的功能。
以下是要运行的常见测试列表。
成功登录.
确保界面组件一致性.
弹出式窗口、排行榜和成就可在 Play 游戏服务用户界面 (UI) 中以各种屏幕尺寸和方向正确且一致地显示。
退出选项在 Play 游戏服务 UI 中不可见。
请确保您可以成功检索玩家 ID,并且(如果适用)服务器端功能按预期工作。
如果您的游戏使用了以下任一功能,请测试它们以确保它们在迁移后仍然像以前一样工作:
如果您的游戏使用了以下任一功能,请测试它们以确保它们在迁移后仍然像以前一样工作:
- 排行榜:提交分数并查看排行榜。检查玩家姓名和分数的正确排名和显示。
- 成就:解锁成就并验证它们是否正确记录并显示在 Play 游戏 UI 中。
- 已保存的游戏:如果游戏使用已保存的游戏,请确保游戏进度保存和加载功能完美运行。这在跨多个设备和应用更新后进行测试尤为重要。
迁移后任务
迁移到 games v2 SDK 后,请完成以下步骤。