Skip to main content

Unity Integration

Gx402 shows how to integrate x402 payments into Unity projects using engine-native UI and wallet connectors (WalletConnect, browser wallets for WebGL). We provide example Unity scripts, a test scene, and an Express middleware example to handle server-side verification and x402 calls.

Prerequisites

  • Unity 2020.3 LTS or newer (2021+ recommended)

High-Level Flow

EVM

  1. Connect Wallet
    • Gx402Manager triggers EVM wallet connect.
    • User approves in MetaMask/Rabby.
    • Address is saved for onchain verification.

  1. Initiate Payment Request
    • Player taps Buy / Call API.
    • Unity triggers HandleApiCallOptimized().
    • Validates EVM address + fetches chain ID.

  1. Build the Transaction
    • Unity prepares an ERC-20 USDC transfer:
    • Resolve token contract for the chain.
    • Convert amount → token units (decimals).
    • ABI-encode transfer(recipient, amount).
    • Estimate gas + set to, data, value=0.

  1. Sign and Send
    • Wallet pops up with transaction details.
    • On approval, Unity sends the tx.
    • Network returns a tx hash (0x…).
    • Unity waits for receipt (status == 1).

  1. Verify and Unlock (x402 Flow)
    • Unity sends the txHash in the X-402-Payment header.
    • Backend checks ERC-20 Transfer event for correct from → to → amount.
    • On success, backend returns 200 OK.
    • Unity displays: ✅ “Payment Verified — Access Granted!”