本文档介绍了如何将现有游戏从 games v1 SDK 迁移到 games v2 SDK。适用于 Unity 的 Play Games 插件(版本 10 及更低版本)使用游戏 v1 SDK。
准备工作
- 确保您已设置 Play 管理中心并安装 Unity Hub。
下载适用于 Unity 的 Google Play 游戏插件
如需使用 Play Games 服务的最新功能,请下载并安装最新版本的插件。从 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
文件导入到项目资源中。 确保当前的 build 平台已设置为 Android。
在主菜单中,依次点击文件 > Build Settings。
选择 Android,然后点击 Switch Platform。
Window > Google Play Games 下应该会显示新的菜单项。如果未显示,请点击 Assets > Refresh 来刷新资源,然后再次尝试设置 build 平台。
在 Unity Hub 中,依次点击 File > Build Settings > Player Settings > Other Settings。
在目标 API 级别框中,选择一个版本。
在 Scripting backend 框中,输入
IL2CPP
。在目标架构框中,选择一个值。
请记下软件包名称 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 Games 服务。
系统会显示欢迎弹出式窗口。
欢迎弹出式窗口示例(点击可放大)。 系统会显示成功日志消息。在终端中运行以下命令:
adb logcat | grep com.google.android.
以下示例显示了一条成功的日志消息:
[
$PlaylogGamesSignInAction$SignInPerformerSource@e1cdecc number=1 name=GAMES_SERVICE_BROKER>], returning true for shouldShowWelcomePopup. [CONTEXT service_id=1 ]
确保界面组件的一致性。
在 Play Games 服务界面 (UI) 中,弹出式窗口、排行榜和成就能在各种屏幕尺寸和方向上正确且一致地显示。
Play Games 服务的界面中未显示退出选项。
确保您可以成功检索玩家 ID,并且服务器端功能(如果适用)可按预期运行。
如果游戏使用服务器端身份验证,请彻底测试
requestServerSideAccess
流程。确保服务器收到授权代码,并且能够使用该代码换取访问令牌。 测试网络错误、无效client ID
场景的成功和失败情况。
如果您的游戏之前使用了以下任何功能,请测试这些功能,以确保它们在迁移后能够正常运行:
- 排行榜:提交得分并查看排行榜。检查玩家名称和得分的排名和显示是否正确。
- 成就:解锁成就并验证它们是否已正确记录并显示在 Play Games 界面中。
- 保存的游戏:如果游戏使用保存的游戏,请确保保存和加载游戏进度能够顺利进行。在多个设备上以及应用更新后进行测试尤为重要。
迁移后的任务
迁移到 Games v2 SDK 后,请完成以下步骤。