सुविधाएं

इस सेक्शन में, Google Play Games PC SDK for Unity के साथ काम करने वाले मुख्य मॉड्यूल को लागू करने के बारे में पूरी जानकारी दी गई है:

डेटा लेयर में इवेंट बनाने की प्रोसेस

किसी भी अन्य सुविधा का इस्तेमाल करने से पहले, SDK टूल को शुरू करना ज़रूरी है. इस प्रोसेस से, आपके Unity गेम और Google Play Games on PC के रनटाइम के बीच कनेक्शन बनता है.

नेमस्पेस: PlayPcSdkManaged.Initialization

Entry Point: GooglePlayInitialization

लागू करना

आपको PlayPcSdkFactory से Unity के लिए खास तौर पर बनाए गए कॉलबैक हैंडलर को वापस पाना होगा. इसके बाद, इसे शुरू करने के तरीके में पास करना होगा. हमारा सुझाव है कि आप इनिशियलाइज़ेशन लॉजिक को सुरक्षित एसिंक रनर में रैप करें, ताकि संभावित अपवादों को मैनेज किया जा सके और दो बार इनिशियलाइज़ेशन को रोका जा सके. इससे यह पक्का किया जा सकेगा कि SDK सही तरीके से काम कर रहा है.

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);
        }
    }
}

गड़बड़ी ठीक करने से जुड़ी जानकारी

एसडीके, एपीआई से मिलने वाले सभी संभावित नतीजों के लिए Result ऑब्जेक्ट का इस्तेमाल करता है. आपको Result.Code की जांच करनी चाहिए, ताकि नेटवर्क की गड़बड़ियों या उपयोगकर्ता के रद्द करने जैसे मामलों को हैंडल किया जा सके.

ध्यान दें: एसडीके एपीआई, लॉजिक से जुड़ी गड़बड़ियों के लिए अपवाद नहीं दिखाते. हालांकि, हमारा सुझाव है कि आप अपने टॉप-लेवल के async तरीकों को try-catch ब्लॉक में रैप करें. इससे यह पक्का होता है कि रनटाइम में अचानक हुई गड़बड़ियों (जैसे कि आपके कोड में शून्य रेफ़रंस या टास्क शेड्यूल करने में गड़बड़ियां) को Unity कंसोल में सही तरीके से लॉग किया गया है.