InAppPurchaseManager (Unity IAP v5)
Overview
Implements a single non‑consumable purchase: Remove Ads (productIdRemoveAds = "removeads"). Caches entitlement in PlayerPrefs for offline first‑launch, exposes Unity Atoms for entitlement gating, and integrates with Ads by disabling requests when the user is entitled.
Atoms / Events
- BoolReference
adsRemoved— entitlement state. - BoolReference
adsCanRequestAds— forcedfalsewhen entitled. - VoidEvent
purchaseRemoveAds— UI “Buy” → triggers purchase. - VoidEvent
onRemoveAdsPurchased— raised after confirmed grant. - UnityEvents —
onInitializationSucceeded/Failed,onPriceUpdated,onPurchaseSucceeded/Failed/Pending,onRestoreSucceeded/Failed,onEntitlementRevoked.
Settings
- useLocalCache (default
true) and cacheKey ("paidVersion") — cache entitlement inPlayerPrefs. - resetOnBootForDebug — clears cache and atoms on boot (for clean QA sessions).
- productIdRemoveAds — Google Play product id (
"removeads").
Public API
bool BootEarlyHint()— reads cached entitlement on Awake to early‑gate Ads (setsadsRemovedandadsCanRequestAds=falseif paid).void BuyRemoveAds()— starts purchase; guarded against “already owned”/“not initialized”.void RestorePurchases()— invokes platform restore API (where supported).bool ShouldInitializeAds()— helper gate (!HasAdsRemoved()).bool HasAdsRemoved()— returns entitlement from live state or early cache.void RefreshEntitlements()—StoreController.FetchPurchases()(also called on app focus).
Lifecycle
Awake()— optional debug reset,DontDestroyOnLoad, BootEarlyHint, register buy event.Start()— clamps atoms from entitlement and InitializePurchasing().- Handles Store events (products fetched, purchases fetched, pending/failed/confirmed) and persists via
SetEntitlement().
Integration Notes
- When entitlement flips to true, the manager sets
adsCanRequestAds=falseso AdMobBootstrap won’t request ads. - The Interstitial policy also checks
PlayerPrefs["paidVersion"]as an extra guard.