將 Google Play 遊戲電腦版 SDK 與 Unity 整合

本指南會逐步說明如何將 Google Play 遊戲電腦版 SDK 整合至 Unity 專案。

步驟 1:下載 SDK

使用下載連結下載最新版 Unity 套件。

下載:Play 遊戲電腦版 Unity SDK

步驟 2:匯入套件

SDK 會以與 Unity Package Manager (UPM) 相容的 tar 檔案形式發布。詳情請參閱「從本機 tar 檔案安裝 UPM 套件

步驟 3:設定建構設定

如要確認原生程式庫是否正確載入,您必須將專案設定為使用 IL2CPP 腳本後端,並指定正確的架構。

  1. 建立建構設定檔,並將平台設為 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. 如要建構遊戲可執行檔,請依序選取「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!」(SDK 初始化成功!) 訊息。