ส่วนนี้มีคู่มือการติดตั้งใช้งานโดยละเอียดสำหรับโมดูลหลัก ที่ SDK ของ Google Play Games บน PC สำหรับ Unity รองรับ
การเริ่มต้น
คุณต้องเริ่มต้น SDK ก่อนที่จะพยายามใช้ฟีเจอร์อื่นๆ กระบวนการนี้จะสร้างการเชื่อมต่อระหว่างเกม Unity กับรันไทม์ของ Google Play Games บน PC
เนมสเปซ: PlayPcSdkManaged.Initialization
จุดแรกเข้า: GooglePlayInitialization
การใช้งาน
คุณต้องเรียกตัวแฮนเดิลการเรียกกลับเฉพาะของ Unity จาก
PlayPcSdkFactory แล้วส่งไปยังเมธอดการเริ่มต้น เราขอแนะนำให้คุณรวมตรรกะการเริ่มต้นไว้ในโปรแกรมเรียกใช้แบบอะซิงโครนัสที่ปลอดภัย
เพื่อจัดการข้อยกเว้นที่อาจเกิดขึ้นและป้องกันการเริ่มต้นซ้ำ
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