AI Integration Agent Instructions for Unity Interstitial Ads
Required Imports
Use the following imports to implement an interstitial ad:
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;Interstitial Ad Workflow
Load the ad:
Use the static
InterstitialAd.Load()method passing the ad unit ID, anAdRequestobject, and a completion callback (Action<InterstitialAd, LoadAdError>):// Test ad unit IDs. Replace with your own ad unit IDs. #if UNITY_ANDROID string adUnitId = "ca-app-pub-3940256099942544/1033173712"; #elif UNITY_IPHONE string adUnitId = "ca-app-pub-3940256099942544/4411468910"; #else string adUnitId = "unused"; #endifInterstitialAd.Load(adUnitId, new AdRequest(), (InterstitialAd ad, LoadAdError error) => { if (error != null || ad == null) { // Handle the error. return; } interstitialAd = ad; });Always destroy any existing
InterstitialAdobject before loading a new ad.Register for ad event callbacks:
Subscribing to ad events on the
InterstitialAdobject is optional and depends on your application requirements:OnAdPaid: Subscribe to log estimated ad revenue (AdValue) for impression-level ad monetization tracking.OnAdImpressionRecorded: Subscribe to track ad impressions in internal telemetry or third-party analytics platforms.OnAdClicked: Subscribe to record user clicks on the ad.OnAdFullScreenContentOpened: Subscribe to pause game audio, timers, or gameplay loops when the interstitial ad covers the screen.OnAdFullScreenContentClosed: Subscribe to resume game audio or gameplay loops when the ad is dismissed. Destroy theInterstitialAdobject (interstitialAd.Destroy()) and reload another ad (InterstitialAd.Load()) so a fresh ad is ready for the next transition point.OnAdFullScreenContentFailed: Subscribe to log display errors if the interstitial ad fails to show.
Ensure background ad callbacks interacting with Unity objects (
UnityEngine) are explicitly scheduled on the main thread usingMobileAdsEventExecutor.ExecuteInUpdate(), because Unity APIs are not thread-safe and must be called from the main thread.interstitialAd.OnAdFullScreenContentOpened += () => { MobileAdsEventExecutor.ExecuteInUpdate(() => { // Interact with UnityEngine objects on the main thread here. }); };Show the ad:
Check
CanShowAd()on theInterstitialAdobject before calling theShow()method. Include a null check to avoid exceptions.if (interstitialAd != null && interstitialAd.CanShowAd()) { interstitialAd.Show(); }Verify the implementation: Ensure there are no compilation errors:
- Verify the Unity Console has no C# script compile errors or missing GoogleMobileAds namespace references.
Links
Additional documentation: