将 Play 游戏服务 Recall 集成到您的应用中

让用户无缝登录您的游戏,同时继续使用您自己的账号系统。借助 Play 游戏服务 Recall API,您可以将游戏内账号与 Google Play 游戏服务账号相关联,这样当用户在不同设备(或在重新安装游戏后在同一设备)上玩您的游戏时,您就可以查询关联的游戏内账号,并简化登录流程。

如果您已集成 Android Recall API,这些 Recall API 应该会看起来很熟悉。由于 Android 和 PC 平台上的 Play 游戏服务 Recall 相同,因此 PC 游戏可以重复使用与 Play 游戏服务 Recall 的任何服务器端集成。

前提条件

第 1 步:在清单中添加您的 Play 游戏服务项目 ID

在 Play 管理中心内完成 Play 游戏服务设置后,您的游戏现在已关联 Play 游戏服务的项目 ID。使用此项目 ID(可在 Play 管理中心的 Play 游戏服务“配置”页面中找到)更新游戏的 manifest.xml

manifest.xml 内容示例:

<?xml version="1.0" encoding="utf-8"?>
<Manifest version="1">
    <Application>
        <PackageName>com.example.package</PackageName>
        <PlayGamesServices>
            <ProjectId>123456789</ProjectId>
        </PlayGamesServices>
    </Application>
</Manifest>

第 2 步:在登录时请求 Recall 访问权限

当您的游戏处理登录流程(例如添加游戏内账号)时,请使用 GamesRecallClient::RequestRecallAccess() 请求 Recall 访问权限。

此调用会返回一个会话 ID,您的后端会使用该 ID 向 Google 发出服务器端调用,以便将您的游戏内账号与 Play 游戏服务用户相关联和取消关联。

auto promise = std::make_shared<std::promise<RecallAccessResult>>();
games_recall_client.RequestRecallAccess(params, [promise](RecallAccessResult result) {
   promise->set_value(std::move(result));
});

auto recall_access_result = promise->get_future().get();
if (recall_access_result.ok()) {
   auto recall_session_id = recall_access_result.value().recall_session_id;
   // Pass the recall session ID to your backend game server so it can query
   // for an existing linked in-game account.
   // - If you discover an existing linked in-game account, continue to sign-in
   //   the in-game account. This provides a seamless cross-device sign-in
   //   experience.
   // - If there is not an existing linked in-game account, when the user
   //   completes the sign-in using your in-game account system record the
   //   account linking with Play Games Services Recall. This helps to provide
   //   a seamless cross-device sign-in experience when the user returns on a
   //   different device or after re-installing your game on the same device.
} else {
   // Handle the error
}

第 3 步:处理 Recall 会话 ID

当您的游戏获得 Recall 会话 ID 并将其传递给后端游戏服务器后,请使用 Play 游戏服务器端 REST API 执行以下操作:

如需有关服务器端集成的更详细指南,请参阅介绍如何在游戏服务器中使用 Recall API 的文档。