このセクションでは、Google Play Games PC SDK for Unity でサポートされているコアモジュールの詳細な実装ガイドについて説明します。
初期化
他の機能を使用する前に SDK を初期化する必要があります。このプロセスにより、Unity ゲームと PC 版 Google Play Games ランタイム間の接続が確立されます。
名前空間: PlayPcSdkManaged.Initialization
エントリ ポイント: GooglePlayInitialization
実装
PlayPcSdkFactory から Unity 固有のコールバック ハンドラを取得し、初期化メソッドに渡す必要があります。安定性を確保するため、初期化ロジックを安全な非同期ランナーでラップして、例外の可能性を処理し、二重初期化を防ぐことをおすすめします。
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("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("SDK Initialized Successfully! "); // You can now create BillingClient or IntegrityClient instances } else { Debug.LogError($"Initialization Failed! "); 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); } } }
エラー処理のリファレンス
SDK は、想定されるすべての API 結果に Result オブジェクトを使用します。ネットワーク エラーやユーザーによるキャンセルなどのシナリオを処理するには、Result.Code を確認する必要があります。
注: SDK API 自体は論理エラーに対して例外をスローしませんが、最上位の async メソッドを try-catch ブロックでラップすることをおすすめします。これにより、予期しないランタイム エラー(独自のコードの null 参照やタスク スケジューリングの失敗など)が Unity コンソールに正しく記録されます。