将 Google Play Games PC SDK 与 Unity 集成

项目,涵盖从 SDK 下载到初始化和 build 配置。 keywords_public: Google Play 游戏电脑版, Unity, SDK 集成, 原生 PC, 游戏开发, IL2CPP, 清单, Play 游戏电脑版 Unity SDK

本指南提供了将 Google Play 游戏电脑版 SDK 集成到 Unity 项目中的分步说明。

第 1 步:下载 SDK

使用下载链接下载最新版本的 Unity 软件包。

下载:Play 游戏电脑版 Unity SDK

第 2 步:导入软件包

该 SDK 以与 Unity 软件包管理器 (UPM) 兼容的 tar 文件的形式分发。如需了解详情, 请参阅从本地 tar 文件安装 UPM 软件包

第 3 步:配置 build 设置

如需验证原生库是否正确加载,您必须将项目配置为使用 IL2CPP 脚本后端并以正确的架构为目标。

  1. 创建一个 build 配置文件Windows 作为平台。

  2. 选择 平台设置 作为 Windows。对于架构,请使用以下选项:

    • Intel 64 位 (推荐)
    • Intel 32 位

    注意 :Google Play 游戏电脑版平台在 64 位环境下运行。 您可以将游戏构建为 32 位 (x86) 或 64 位 (x64)。

  3. Scripting Backend 设置为 IL2CPP 。 如需了解详情,请参阅使用 IL2CPP 构建项目

    • Api Compatibility Level 设置为 .NET Standard 2.0 (或 .NET Framework)。

第 4 步:创建应用清单

如需在游戏中使用 SDK,您必须将游戏可执行文件与您在 Play 管理中心 内声明的 Play 软件包名称相关联。为此,请在游戏可执行文件所在的同一目录下添加 manifest.xml 文件。

注意 :这是一个必须手动执行的步骤。

  1. 如需构建游戏可执行文件,请依次选择文件 > Build and Run 或按 Ctrl+B
  2. 打开文本编辑器并创建一个名为 manifest.xml 的新文件。
  3. 将以下 XML 代码复制并粘贴到该文件中:

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

  4. 将该文件另存为 manifest.xml

  5. 将此文件移到已构建的游戏可执行文件所在的文件夹中。

    示例:如果您的游戏位于 Builds/MyGame.exe,则清单必须位于 Builds/manifest.xml

注意 :如果您想在 Unity 编辑器中开发时使用 PC SDK,而无需对游戏可执行文件进行数字签名或从 Google Play 游戏启动它。如需了解其他清单配置步骤,请参阅 开发者模式设置指南

第 5 步:初始化 SDK

您必须先初始化 SDK,然后才能访问任何功能,例如结算或完整性。使用 PlayPcSdkFactory 创建初始化处理程序并启动连接。

创建一个新的 C# 脚本(例如 SdkInitialization.cs),并添加以下代码:

using UnityEngine;
using System;
using System.Threading.Tasks;
// Import the SDK namespaces
using PlayPcSdkManaged.Initialization;
using PlayPcSdkManaged.Unity;

public class GooglePlayPCSDKInit : MonoBehaviour
{
    // Prevent double-initialization if this script is reloaded
    private static bool _isInitialized = false;

    private void Start()
    {
        // Use the "Safe Runner" pattern to fire the async method
        _ = InitializeSdkAsync();
    }

    private async Task InitializeSdkAsync()
    {
        if (_isInitialized)
        {
            Debug.LogWarning("Google Play PC SDK is already initialized. Skipping.");
            return;
        }

        try
        {
            Debug.Log("Initializing Google Play PC SDK...");

            // 1. Get the Unity-specific initialization handler from the factory
            var initHandler = PlayPcSdkFactory.InitializationHandler;

            // 2. Call InitializeAsync to start the connection
            var result = await GooglePlayInitialization.InitializeAsync(initHandler);

            // 3. Check the result
            if (result.IsOk)
            {
                _isInitialized = true;
                Debug.Log("<color=green>Google Play PC SDK Initialized Successfully!</color>");
                // You can now create BillingClient or IntegrityClient instances
            }
            else
            {
                Debug.LogError($"<color=red>Initialization Failed!</color>");
                Debug.LogError($"Error Code: {result.Code}");
                Debug.LogError($"Message: {result.ErrorMessage}");
            }
        }
        catch (Exception ex)
        {
            // Catch unexpected crashes or task failures
            Debug.LogError($"Exception during initialization: {ex.Message}");
            Debug.LogException(ex);
        }
    }
}

将此脚本附加到第一个场景中的 GameObject 。运行游戏时,请检查控制台是否显示“SDK Initialized Successfully!”消息。