Banner views are rectangular image or text ads that occupy a spot on screen. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you're new to mobile advertising, they're a great place to start. Case study.
This guide shows you how to integrate banner views into a Unity app. In addition to code snippets and instructions, it also includes information about sizing banners properly and links to additional resources.
Prerequisites
- Complete the Get started guide.
Always test with test ads
The following sample code contains an ad unit ID which you can use to request test ads. It's been specially configured to return test ads rather than production ads for every request, making it safe to use.
However, after you've registered an app in the AdMob web interface and created your own ad unit IDs for use in your app, explicitly configure your device as a test device during development.
Android
ca-app-pub-3940256099942544/6300978111
iOS
ca-app-pub-3940256099942544/2934735716
Initialize the Mobile Ads SDK
Before loading ads, have your app initialize the Mobile Ads SDK by calling
MobileAds.Initialize()
. This needs to be done only once, ideally at app launch.
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
}
If you're using mediation, wait until the callback occurs before loading ads as this will ensure that all mediation adapters are initialized.
BannerView example
The following sample code details how to use the banner view. In the example,
create an instance of a banner view, use an
AdRequest
to load an ad into the banner view, and
then extend its capabilities by handling lifecycle events.
Create a banner view
The first step in using a banner view is to create an instance of a banner view.
Replace AD_UNIT_ID with your ad unit ID.
The constructor for a BannerView
has the following
parameters:
adUnitId
: The ad unit ID of the banner ad to load.AdSize
: The banner size you'd like to use.AdPosition
: The position where the banner views should be placed.
(Optional) Create a banner view with a custom position
For greater control over where a banner view is placed on screen than what's
offered by AdPosition
values, use the constructor
that has x- and y-coordinates as parameters:
The top-left corner of the banner view is positioned at the x and y values passed to the constructor, where the origin is the top-left of the screen.
(Optional) Create a banner view with a custom size
In addition to using an AdSize
constant, you can also specify a custom size
for your ad:
Load a banner ad
To load an ad, create an AdRequest
and pass it to
the LoadAd()
method.
// Send a request to load an ad into the banner view.
bannerView.LoadAd(new AdRequest());
Listen to banner view events
To customize the behavior of your ad, you can hook into a number of events in the ad's lifecycle, such as loading, opening, or closing. To listen for these events, register a delegate:
bannerView.OnBannerAdLoaded += () =>
{
// Raised when an ad is loaded into the banner view.
};
bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
{
// Raised when an ad fails to load into the banner view.
};
bannerView.OnAdPaid += (AdValue adValue) =>
{
// Raised when the ad is estimated to have earned money.
};
bannerView.OnAdImpressionRecorded += () =>
{
// Raised when an impression is recorded for an ad.
};
bannerView.OnAdClicked += () =>
{
// Raised when a click is recorded for an ad.
};
bannerView.OnAdFullScreenContentOpened += () =>
{
// Raised when an ad opened full screen content.
};
bannerView.OnAdFullScreenContentClosed += () =>
{
// Raised when the ad closed full screen content.
};
Destroy the banner view
When finished using the banner view, be sure to call Destroy()
to release
resources.
if (bannerView != null)
{
// Always destroy the banner view when no longer needed.
bannerView.Destroy();
bannerView = null;
}
That's it! Your app is now ready to display banner ads.
Refresh an ad
If you configured your ad unit to refresh, you don't need to request another ad when the ad fails to load. Google Mobile Ads SDK respects any refresh rate you specified in the AdMob UI. If you haven't enabled refresh, issue a new request. For more details on ad unit refresh, such as setting a refresh rate, see Use automatic refresh for Banner ads.
Banner sizes
The following table lists the standard banner sizes:
Size in dp (WxH) | Description | Availability | AdSize constant |
---|---|---|---|
320x50 | Standard Banner | Phones and Tablets | BANNER |
320x100 | Large Banner | Phones and Tablets | LARGE_BANNER |
300x250 | IAB Medium Rectangle | Phones and Tablets | MEDIUM_RECTANGLE |
468x60 | IAB Full-Size Banner | Tablets | FULL_BANNER |
728x90 | IAB Leaderboard | Tablets | LEADERBOARD |
Provided width x Adaptive height | Adaptive banner | Phones and Tablets | N/A |
Screen width x 32|50|90 | Smart banner | Phones and Tablets | SMART_BANNER |
Learn more about Adaptive Banners, intended to replace Smart Banners. |
Additional resources
- HelloWorld example: A minimal implementation of all ad formats.