AI Integration Agent Instructions for Unity Banner Ads
Required Imports
Use the following imports to implement a banner ad:
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;Banner Ad Workflow
Define the ad view:
Use the
BannerViewclass fromGoogleMobileAds.Apifor defining banner ads. Set the appropriate test ad unit ID when creating the ad view:- Android anchored adaptive:
ca-app-pub-3940256099942544/9214589741 - iOS anchored adaptive:
ca-app-pub-3940256099942544/2435281174
- Android anchored adaptive:
Set the ad size:
Pass
AdSize.Banneror the appropriate adaptive ad size to theBannerViewconstructor along withAdPosition(AdPosition.ToporAdPosition.Bottom).For an anchored adaptive banner, use a large anchored adaptive ad size unless the user complains or a small height is needed.
int width = MobileAds.Utils.GetDeviceSafeWidth(); AdSize adSize = AdSize .GetCurrentOrientationLargeAnchoredAdaptiveBannerAdSizeWithWidth(width); BannerView bannerView = new BannerView(_adUnitId, adSize, AdPosition.Bottom);Register for ad load events:
Subscribing to ad events on the
BannerViewobject is optional and depends on your application requirements:OnBannerAdLoaded: Subscribe to update UI state when the banner ad is ready and displayed.OnBannerAdLoadFailed: Subscribe to log errors if the banner ad fails to load.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 banner click opens full-screen destination content.OnAdFullScreenContentClosed: Subscribe to resume game audio or gameplay loops when full-screen destination content is dismissed.
CRITICAL: Whenever you provide code or instructions for Unity ads, you MUST explicitly state that background ad callbacks interacting with Unity objects (
UnityEngine) must be scheduled on the main thread usingMobileAdsEventExecutor.ExecuteInUpdate(), because Unity APIs are not thread-safe.bannerView.OnBannerAdLoaded += () => { MobileAdsEventExecutor.ExecuteInUpdate(() => { // Interact with UnityEngine objects on the main thread here. }); };Load the banner ad:
Call the
LoadAd()method on theBannerViewobject passing a newAdRequestobject (bannerView.LoadAd(new AdRequest())).bannerView.LoadAd(new AdRequest());Verify the implementation:
Compile the Unity csproj file by running
dotnet build. Resolve any GMA SDK related compile errors.
Links
Additional documentation: