使用 Play 游戏电脑版 SDK 为您的游戏注入强劲动力

借助 Play 游戏电脑版 SDK,您可以访问 Google Play 服务,在 PC 上构建游戏并变现。使用 Play 结算服务销售数字内容,使用 Play 游戏服务实现无缝登录,并使用 Play Integrity 验证用户是否拥有应用的有效使用权。

如需简要了解即将推出的功能、稳定版本和集成架构,请参阅 Google Play 游戏电脑版路线图。

准备好开始使用了吗?

前提条件

  • 在 Play 管理中心内创建应用条目,并声明 Play 软件包名称。

  • 下载并安装 Google Play 游戏电脑版,然后使用您的 Google 账号登录。

第 1 步:将 SDK 添加到您的项目

C++

  • 下载 Play 游戏电脑版 C++ SDK。

  • 将 API 标头文件夹 includes/ 复制到应用的代码库中。

  • 根据您的目标架构,将可再分发文件从 imports/ 目录复制到应用的项目中:

  • 对于 64 位 (x64):复制 imports/x64/ 中的文件。

  • 对于 32 位 (x86):复制 imports/x86/ 中的文件。

  • 将您的项目与 play_pc_sdk.lib 关联,以允许访问 play_pc_sdk.dll 的内容。

C#

第 2 步:添加清单文件

您需要先将游戏可执行文件与您在 Play 管理中心内声明的 Play 软件包名称相关联,然后才能在游戏中使用该 SDK。具体方法是在游戏可执行文件所在的目录中添加 manifest.xml 文件。

manifest.xml 内容示例:

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

manifest.xml 展示位置示例:

C:\Program Files
└───Example Game
    ├───Game.exe
    └───manifest.xml

第 3 步:对游戏进行数字签名

游戏必须先使用 Authenticode 数字签名对游戏的可执行文件进行数字签名,然后才能使用该 SDK。如需了解如何对可执行文件进行签名,请参阅有关 SignTool 的文档。

完成游戏数字签名流程后,请将证书信息发送给您的 Google 代表(或在入驻期间通过电子邮件联系 google-play-games-pc@google.com)进行配置。

第 4 步:初始化 SDK

在游戏的启动序列期间初始化 SDK。此操作应自动完成,无需任何用户互动,并且建议您在呈现游戏窗口之前验证是否已成功初始化。 这样可以尽快发现并解决错误,从而提供最佳用户体验,并避免在游戏进程需要退出时短暂显示游戏窗口。

通过调用 GooglePlayInitialize (C++) 或 GooglePlayInitialization.InitializeAsync (C#) 初始化 API,开始使用 SDK。我们建议您在初始化期间将游戏的 Android 软件包名称传入 GooglePlayInitializeParams。此操作会设置全局状态、连接到 SDK 运行时,并验证应用是否已正确启动。您必须调用此方法,并使延续回调完成,且 InitializeResult::ok() (C++) 或 Result.IsOk (C#) 等于 true,然后才能使用任何其他 API。

C++

// Initialize the SDK as part of the startup sequence of your
// application.
auto promise = std::make_shared<std::promise<InitializeResult>>();
GooglePlayInitializeParams params;
// Replace with your game's package name.
params.package_name = "com.example.game";
GooglePlayInitialize(
  [promise] (InitializeResult result) {
    promise->set_value(std::move(result));
  }, params);

auto initialize_result = promise->get_future().get();
if (initialize_result.ok()) {
  // The SDK succeeded with initialization. Continue with the startup
  // sequence of the game.
  // ...

  // Retrieve the operating environment and device form factor.
  auto env = initialize_result.value().environment;
  auto form_factor = initialize_result.value().form_factor;
  // ...
} else if (
    initialize_result.code() ==
    InitializationError::kActionRequiredShutdownClientProcess) {
  // The SDK failed to initialize and has requested that your game
  // process exit as soon as possible.
  exit(1);
} else {
  // The SDK failed to initialize for an alternative reason. It is
  // still generally recommended that you exit the game process as
  // soon as possible, because it won't be possible to access any
  // APIs in the SDK. Critical operations such as verifying the user
  // owns a valid license to your game won't be possible.
  // ...
}

C#

// SDK Clients
private BillingClient _billingClient;
private IntegrityClient _integrityClient;

// Stored product information
private string _offerToken;

private async void InitializeSDK()
{
    // The factory provides the necessary handler for initialization.
    var initializationHandler =
        PlayPcSdkFactory.InitializationHandler;
    var initializeParams = new GooglePlayInitializeParams {
        // Replace with your game's package name.
        PackageName = "com.example.game"
    };
    var result = await GooglePlayInitialization.InitializeAsync(
        initializationHandler, initializeParams);

    if (result.IsOk)
    {
        // Use the factory to get Unity-compatible instances of the
        // clients
        _billingClient = PlayPcSdkFactory.CreateBillingClient();
        _integrityClient = PlayPcSdkFactory.CreateIntegrityClient();

        // Retrieve the operating environment and device form factor.
        var env = result.Value.Environment;
        var form_factor = result.Value.FormFactor;

        // SDK is ready for use
    }
    else
    {
        // Handle specific, actionable errors
        if (result.Code ==
            InitializationError.ActionRequiredShutdownClientProcess)
        {
            Log("This game must be launched through the Google Play Games client. Exit all game processes immediately, GPG will relaunch the game safely.");
            Application.Quit();
        }
    }
}

如果初始化失败,并显示代码 kActionRequiredShutdownClientProcess (C++) / InitializationError.ActionRequiredShutdownClientProcess (C#),请尽快退出游戏进程。SDK 的运行时将尝试协助用户,而无需您的游戏执行任何其他操作。例如,如果用户没有游戏的有效许可,Google Play 游戏会提示用户购买。对于其他错误,您也应退出游戏进程,因为您将无法使用 SDK 执行关键操作,例如验证用户是否拥有游戏的有效许可。

非成功响应可能表示以下情况之一:

  • SDK 运行时未安装、未在设备上运行,或者版本较旧,与集成到游戏中的 SDK 不兼容。

  • SDK 运行时无法验证游戏的 应用身份。这可能是因为 manifest.xml 无效,或者在开发时使用 SDK 但未启用开发者模式。否则,您需要使用已注册到 Google Play 软件包名称的数字证书对游戏的可执行文件进行数字签名。

  • 游戏可执行文件不是通过 Google Play 游戏客户端启动的。

  • Google Play 游戏中的活跃用户不具备相应应用的许可。

第 5 步:(可选)支持多个游戏进程

如果您的游戏使用多个进程,并且 Play 游戏电脑版 SDK 使用的进程与 Google Play 游戏电脑版启动的进程不同,请完成以下额外的集成步骤。例如,如果 Google Play Games on PC 启动了游戏的启动器,然后启动器启动了将与 SDK 交互的游戏进程。

  1. 每个使用 Play 游戏电脑版 SDK 的进程都必须验证 Play 游戏电脑版 SDK 是否已成功初始化。

    如果由 Google Play 游戏电脑版直接启动的进程使用该 SDK,则在该进程中初始化该 SDK 可尽快显示错误,从而提供最佳用户体验。如果直接启动的进程是仅转发命令行参数而不调用 SDK 的直通启动器或引导程序,则只有初始化 SDK 的子进程才需要在其可执行目录中包含 manifest.xml。如果直接启动的进程和子进程都初始化 SDK,则每个进程都必须执行初始化,并在其自己的可执行目录中包含 manifest.xml。例如,在 Unreal Engine 拆分可执行文件 build 中,只有发布二进制文件会初始化 SDK,请将 manifest.xml 放在 <Game>\Binaries\Win64\` alongside*-Win64-Shipping.exe` 中:

    C:\Program Files\Example Game
    ├───ExampleGame.exe
    └───ExampleGame\Binaries\Win64
        ├───ExampleGame-Win64-Shipping.exe
        └───manifest.xml
    
  2. 如需在子进程中使用 Play 游戏电脑版 SDK,请将命令行参数转发到生成的子进程。

    命令行参数转发示例:

    Processes hierarchy tree:
    
    GooglePlayGames.exe
    └───YourGameLauncher.exe --gpg_args=abc --your_args=123
        └───YourGame.exe --gpg_args=abc --your_args=123
    

    在此示例中,我们看到了一个进程层次结构,其中 Google Play 游戏电脑版 (GooglePlayGames.exe) 使用一些示例参数 (--gpg_args=abc --your_args=123) 启动游戏 (YourGameLauncher.exe)。然后,游戏会生成一个使用 Play 游戏电脑版 SDK 的子进程 (YourGame.exe)。为实现此目的,由 Google Play 游戏电脑版启动的游戏进程会将收到的命令行参数转发给子进程。

  3. 在游戏停止运行时退出所有进程。

    当用户关闭游戏或游戏因 SDK 初始化失败(例如 kActionRequiredShutdownClientProcess)而退出时,请关闭游戏生成的所有进程。这样可以确保下次 Google Play 游戏电脑版客户端启动您的游戏时,新更改(例如切换到其他有效账号)会生效。

后续步骤

在 IDE 中开发时使用 SDK:

为您的应用添加 Google Play PC 版功能: