Play Integrity

Play Integrity API ปกป้องเกมของคุณจากการโต้ตอบที่มีความเสี่ยงและเป็นการฉ้อโกง ด้วยโทเค็นการตัดสินความสมบูรณ์ที่คุณยืนยันในเซิร์ฟเวอร์แบ็กเอนด์

เนมสเปซ: PlayPcSdkManaged.Integrity

คลาสไคลเอ็นต์: IntegrityClient

สร้างไคลเอ็นต์

ใช้โรงงานเพื่อสร้าง IntegrityClient Factory จะลงทะเบียนการเรียกกลับที่เฉพาะเจาะจงของ Unity และทิ้งไคลเอ็นต์เมื่อสคริปต์ถูกทำลาย

using UnityEngine;
using System;
using System.Threading.Tasks;
// Required SDK Namespaces
using PlayPcSdkManaged.Integrity;
using PlayPcSdkManaged.Unity;

public class IntegrityManager : MonoBehaviour
{
    private IntegrityClient _integrityClient;

    public void SetupIntegrity()
    {
        try
        {
            // Creates the client with the required UnityIntegrityCallbacksHandler
            _integrityClient = PlayPcSdkFactory.CreateIntegrityClient();
            Debug.Log("Integrity Client created successfully.");
        }
        catch (Exception ex)
        {
            Debug.LogError($"Failed to create Integrity Client: {ex.Message}");
        }
    }

    private void OnDestroy()
    {
        // Always dispose of the client
        _integrityClient?.Dispose();
    }
}

ขอโทเค็นความสมบูรณ์

การขอโทเค็นความสมบูรณ์เป็นกระบวนการ 2 ขั้นตอน ดังนี้

  • เตรียมพร้อม เตรียมบริการตรวจสอบความสมบูรณ์และสร้างรหัสเซสชัน

  • คำขอ ใช้รหัสเซสชันจากขั้นตอนการเตรียมข้อมูลเพื่อขอโทเค็นที่ลงชื่อจริง

public async Task RequestIntegrityTokenAsync()
{
    try
    {
        // Replace with your actual Google Cloud Project Number
        long myCloudProjectNumber = 123456789;

        // ---------------------------------------------------------
        // Step 1: Prepare the Token
        // ---------------------------------------------------------
        var prepareParams = new PrepareIntegrityTokenParams
        {
            CloudProjectNumber = myCloudProjectNumber
        };

        Debug.Log("Preparing Integrity Token...");
        var prepareResult = await _integrityClient.PrepareIntegrityTokenAsync(prepareParams);

        if (!prepareResult.IsOk)
        {
            Debug.LogError($"Prepare Failed: {prepareResult.Code} - {prepareResult.ErrorMessage}");
            return;
        }

        // ---------------------------------------------------------
        // Step 2: Request the Token
        // ---------------------------------------------------------
        // You must use the RequestTokenData returned from the prepare step
        var requestParams = new RequestIntegrityTokenParams
        {
            RequestTokenData = prepareResult.Value.RequestTokenData,

            // A nonce to bind this token to a specific request or action.
            // This should be generated by your backend server.
            RequestHash = "your_secure_nonce_string"
        };

        Debug.Log("Requesting Integrity Token...");
        var tokenResult = await _integrityClient.RequestIntegrityTokenAsync(requestParams);

        if (tokenResult.IsOk)
        {
            // The TokenBytes is already a string (Base64 URL-safe web token)
            string signedToken = tokenResult.Value.TokenBytes;

            Debug.Log("Integrity Token Received. Sending to backend for verification...");

            // Send 'signedToken' to your backend server for verification
        }
        else
        {
            Debug.LogError($"Request Failed: {tokenResult.Code} - {tokenResult.ErrorMessage}");
        }
    }
    catch (Exception ex)
    {
        Debug.LogException(ex);
    }
}