Google Play Games PC SDK を Unity と統合する

このガイドでは、Google Play Games PC SDK を Unity プロジェクトに統合する手順について説明します。

ステップ 1: SDK をダウンロードする

ダウンロード リンクを使用して、Unity パッケージの最新バージョンをダウンロードします。

ダウンロード: Play Games PC Unity SDK

ステップ 2: パッケージをインポートする

SDK は、Unity Package Manager(UPM)と互換性のある tar ファイルとして配布されます。詳細については、ローカル tar ファイルから UPM パッケージをインストールするをご覧ください。

ステップ 3: ビルド設定を構成する

ネイティブ ライブラリが正しく読み込まれることを確認するには、IL2CPP スクリプト バックエンドを使用し、正しいアーキテクチャをターゲットとするようにプロジェクトを構成する必要があります。

  1. プラットフォームとして Windows を使用して、ビルド プロファイルを作成します。

  2. プラットフォーム設定として Windows を選択します。アーキテクチャの場合は、次のオプションを使用します。

    • Intel 64 ビット(推奨)
    • Intel 32 ビット

    注: PC 版 Google Play Games プラットフォームは 64 ビット環境で動作します。ゲームは 32 ビット(x86)または 64 ビット(x64)としてビルドできます。

  3. [Scripting Backend] を [IL2CPP] に設定します。詳細については、IL2CPP を使用したプロジェクトのビルドをご覧ください。

    • [Api Compatibility Level] を [.NET Standard 2.0](または .NET Framework)に設定します。

ステップ 4: アプリケーション マニフェストを作成する

ゲームで SDK を使用する前に、ゲームの実行可能ファイルを Google Play Console で申請した Google Play パッケージ名に関連付ける必要があります。これを行うには、ゲームの実行可能ファイルと同じディレクトリに manifest.xml ファイルを追加します。

注: これは手動で行う必要がある手順です。

  1. ゲームの実行可能ファイルをビルドするには、[ファイル] > [ビルドして実行] を選択するか、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 が正常に初期化されました)というメッセージを確認します。