בקטע הזה מופיעים מדריכי הטמעה מפורטים למודולים העיקריים שנתמכים על ידי Google Play Games PC SDK ל-Unity:
אתחול
צריך להפעיל את ה-SDK לפני שמנסים להשתמש בתכונות אחרות. התהליך הזה יוצר את החיבור בין המשחק ב-Unity לבין זמן הריצה של Google Play Games במחשב.
מרחב שמות: PlayPcSdkManaged.Initialization
נקודת כניסה: GooglePlayInitialization
הטמעה
צריך לאחזר את handler של הקריאה החוזרת שספציפי ל-Unity מ-PlayPcSdkFactory ולהעביר אותו לשיטת האתחול. כדי להבטיח יציבות, מומלץ לעטוף את לוגיקת האתחול ב-safe async runner כדי לטפל בחריגים פוטנציאליים ולמנוע אתחול כפול.
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 משתמשת באובייקט Result לכל התוצאות הצפויות של ה-API. כדאי לבדוק את Result.Code כדי לטפל בתרחישים כמו שגיאות ברשת או ביטולים של משתמשים.
הערה: ממשקי ה-API של ה-SDK לא יוצרים חריגים לשגיאות לוגיות, אבל עדיין מומלץ להוסיף את שיטות async ברמה העליונה לבלוק try-catch. כך אפשר לוודא ששגיאות לא צפויות בסביבת זמן הריצה (כמו הפניות לערך null בקוד שלכם או כשלים בתזמון משימות) מתועדות בצורה נכונה במסוף Unity.