Native express ads are ads where native ad assets (the text and images that
make up an ad) are combined with a CSS template at the server level and
displayed in a native express ad view within an app. Publishers customize
their ad presentations by modifying the CSS template associated with each
of their ad units. AdMob combines that template with advertiser assets like
icons, images, and text, and displays the results in a NativeExpressAdView
.
This approach minimizes the amount of mobile code needed for a Native Express
ad, while helping publishers display ads that look native to their app.
Create a native express ad unit
Native express ad units are created at apps.admob.com. For an overview of the format and more information on choosing a template size for your ad units, see this help center article. For more information on how to create CSS to give your Native Express ads a natural, unobtrusive style, see Guide to custom CSS for native ads express.
Basic native express ad request
The NativeExpressAdView
class is responsible for requesting and displaying
native express ads. Here's the minimal code needed to create and load a
native express ad:
private void RequestNativeExpressAdView()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/1072772517";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 native express ad at the top of the screen.
nativeExpressAdView = new NativeExpressAdView(adUnitId, new AdSize(320, 150), AdPosition.Top);
// Load a banner ad.
nativeExpressAdView.LoadAd(new AdRequest.Builder().Build());
}
Native express ad events
NativeExpressAdView
provides ad events to notify you about an ad's
lifecycle. These events are of type
EventHandler.
This example demonstrates how to register for ad events on a native express ad:
...
// Called when an ad request has successfully loaded.
nativeExpressAdView.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request failed to load.
nativeExpressAdView.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Called when an ad is clicked.
nativeExpressAdView.OnAdOpened += HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
nativeExpressAdView.OnAdClosed += HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
nativeExpressAdView.OnAdLeavingApplication += HandleOnAdLeavingApplication;
...
public void HandleOnAdLoaded(object sender, EventArgs args)
{
print("Ad loaded");
// Handle the ad failed to load event.
}
Each ad event uses EventArgs
with the exception of OnAdFailedToLoad
. The OnAdFailedToLoad
event
uses AdFailedToLoadEventArgs
and includes a reason for why the ad failed to load:
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("Ad failed to load: " + args.Message);
// Handle the ad failed to load event.
}
Clean up native express ads
When you are finished with a NativeExpressAdView
, make sure to call
the Destroy()
method before dropping your reference to it:
nativeExpressAdView.Destroy();
This notifies the plugin that the object is no longer used and the memory it occupied can be reclaimed. Not calling this method results in memory leaks.