将 Google Play Games PC SDK 与 Unity 集成

本指南提供了将 Google Play Games PC SDK 集成到 Unity 项目中的分步说明。

第 1 步:下载 SDK

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

下载:Play Games 电脑版 Unity SDK

第 2 步:导入软件包

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

第 3 步:配置 build 设置

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

  1. 创建以 Windows 为平台的 build 配置文件

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

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

    注意:Google Play Games 电脑版平台在 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. 如需构建游戏可执行文件,请选择 File > 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

第 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!”消息。