기능

이 섹션에서는 Unity용 Google Play 게임즈 PC SDK에서 지원하는 핵심 모듈에 관한 자세한 구현 가이드를 제공합니다.

초기화

다른 기능을 사용하기 전에 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 콘솔에 올바르게 로깅됩니다.