Tài liệu

Hỗ trợ

Banner integration for Unity

Integrate banner ads in Unity by initializing the SDK, creating banner ad objects post-initialization, and setting appropriate ad sizes and positions.
Thời gian đọc 4 phútCập nhật lần cuối 2 ngày trước

Banners are a rectangular, system-initiated ads that can be either static or animated, and are served in a designated area around your live app content. 

Prerequisites

  • Ensure that you have correctly integrated the ironSource SDK into your application. Integration is outlined here.
  • Ensure that you initialize the SDK using LevelPlay Initialization API.
  • Find the AdUnitID in LevelPlay dashboard.

Create Banner Ad Object, Set Size and Position

The creation of the banner ad object should be done after receiving OnInitSuccess callback.
// Create the banner object and set the ad unit id 
LevelPlayBannerAd bannerAd = new LevelPlayBannerAd(bannerAdUnitId);
Note: the
LevelPlayBannerAd
constructor also supports an optional
Config
parameter:
// Use optional Config parameter
LevelPlayBannerAd(string adUnitId, Config config = null)
If you do not require special settings, you can omit the
Config
parameter and pass only the
adUnitId
. The banner will be created with default attributes:

Attribute

Default

SizeBanner
PositionBottomCenter
displayOnLoadtrue
respectSafeAreafalse (Android only)
To customize banner parameters, use the
Config
builder:
var configBuilder = new LevelPlayBannerAd.Config.Builder();
configBuilder.SetSize(LevelPlayAdSize.LARGE);
configBuilder.SetPosition(LevelPlayBannerPosition.TopCenter);
configBuilder.SetDisplayOnLoad(true);
configBuilder.SetRespectSafeArea(true); // Only relevant for Android
configBuilder.SetPlacementName("bannerPlacement");
configBuilder.SetBidFloor(1.0); // Minimum bid price in USD
var bannerConfig = configBuilder.Build();

bannerAd = new LevelPlayBannerAd(bannerAdUnitId, bannerConfig);

Banner Sizes

LevelPlayAdSize

Description

Dimensions in dp (Width X Height)

BANNERStandard banner320 x 50
LARGELarge banner320 x 90
MEDIUM_RECTANGLEMedium Rectangular (MREC)300 x 250
AdaptiveAutomatically renders ads to adjust size and orientation for mobile & tabletsDevice width X recommended height
To create the ad size, follow one of these options: Adaptive ad size that adjusts to the screen width (recommended): This option returns BANNER or LEADERBOARD according to the device type. Networks that support the adaptive feature (Google, Yandex) will return a height based on their optimization logic.
  • For LevelPlay SDK 8.8.0+ you can determine the width and height of the adaptive banner that will be returned, before loading the banner, using the following:
LevelPlayAdSize adSize = LevelPlayAdSize.CreateAdaptiveAdSize();
int width = adSize.Width;
int height = adSize.Height;
  • For LevelPlay SDK below 8.8.0:
LevelPlayAdSize adSize = LevelPlayAdSize.CreateAdaptiveAdSize();
Specific banner size: This option allows you to set a specific banner size: BANNER, LARGE, MEDIUM_RECTANGLE.
LevelPlayAdSize adSize = LevelPlayAdSize.BANNER;

Banner Positions

The default position for displaying a Banner on the screen is BottomCenter. Below is the complete list of all supported positions:
  • TopLeft
  • TopCenter
  • TopRight
  • CenterLeft
  • Center
  • CenterRight
  • BottomLeft
  • BottomCenter
  • BottomRight
LevelPlayBannerAd bannerAd = new LevelPlayBannerAd(adUnitId, LevelPlayAdSize.BANNER, LevelPlayBannerPosition.TopLeft);
You can also position a Banner at a specific (x, y) coordinate on the screen by creating a
LevelPlayBannerPosition
object, with x and y values defined in dp.
LevelPlayBannerPosition position = new LevelPlayBannerPosition(new Vector2(x, y));
LevelPlayBannerAd bannerAd = new LevelPlayBannerAd(adUnitId, adSize, position, placementName, displayOnLoad, respectSafeArea);

Placements

We support placements in banners for reporting only. They should be set before the
LoadAd
to affect all reloaded ads.
// Set the placement name
LevelPlayBannerAd bannerAd = new LevelPlayBannerAd("bannerAdUnitId", LevelPlayAdSize.BANNER, placementName:"placementName");

Implement the Banner Events

Listen to the
LevelPlayBannerAd
events in your code. The SDK will notify all possible events listed below.
  • It is recommended to set the listener before loading the banner ad.
  • Each banner ad should have its own listener implementation.
  • Callbacks run on the main thread.
// Register to the events 
bannerAd.OnAdLoaded += BannerOnAdLoadedEvent;
bannerAd.OnAdLoadFailed += BannerOnAdLoadFailedEvent;
bannerAd.OnAdDisplayed += BannerOnAdDisplayedEvent;
bannerAd.OnAdDisplayFailed += BannerOnAdDisplayFailedEvent;
bannerAd.OnAdClicked += BannerOnAdClickedEvent;
bannerAd.OnAdCollapsed += BannerOnAdCollapsedEvent;
bannerAd.OnAdLeftApplication += BannerOnAdLeftApplicationEvent;
bannerAd.OnAdExpanded += BannerOnAdExpandedEvent;

// Implement the events
void BannerOnAdLoadedEvent(LevelPlayAdInfo adInfo) {}
void BannerOnAdLoadFailedEvent(LevelPlayAdError ironSourceError) {}
void BannerOnAdClickedEvent(LevelPlayAdInfo adInfo) {}
void BannerOnAdDisplayedEvent(LevelPlayAdInfo adInfo) {}
void BannerOnAdDisplayFailedEvent(LevelPlayAdInfo adInfo, LevelPlayAdError error) {}
void BannerOnAdCollapsedEvent(LevelPlayAdInfo adInfo) {}
void BannerOnAdLeftApplicationEvent(LevelPlayAdInfo adInfo) {}
void BannerOnAdExpandedEvent(LevelPlayAdInfo adInfo) {}

LevelPlay Ad Info

The
LevelPlayAdInfo
parameter includes information about the loaded ad.

Load Banner Ad

To load a banner ad use
LoadAd
.
bannerAd.LoadAd();

Pause and Resume Banner Refresh

You can pause banner refresh in your code if the refresh value was defined in the platform. Use the following methods to stop the automatic refresh of the banner ad, or re-enable it after pausing. Note: When the banner is displayed again, it will complete the time till refresh, from the time it was paused.
// Pause refresh
bannerAd.PauseAutoRefresh();
// Resume refresh
bannerAd.ResumeAutoRefresh();
bannerAd.HideAd();

Hide and Show Banners

As part of the banner constructor, you can load your banner in the background and show it on screen only when relevant. To control the ad visibility after loading, you can use these APIs:
// Show ad 
bannerAd.ShowAd(); 
// Hide ad 
bannerAd.HideAd();

Display Cutouts (Android Only)

A display cutout in Android devices is a designated area reserved for essential components such as cameras, sensors, or speakers, commonly used in smartphones and devices with edge-to-edge displays. The cutout can potentially limit the game view, affecting the placement of banners on the screen. To avoid overlap of the banner ad and the displayed cutouts, create
LevelPlayBannerAd
and set the
respectSafeArea
to true.
You can learn more about Google’s solution for display cutout here. To support Android cutouts, create a banner object using
Config
and set the respectSafeArea value as true (default value is false).
var configBuilder = new LevelPlayBannerAd.Config.Builder();
configBuilder.SetRespectSafeArea(true); // Only relevant for Android
var bannerConfig = configBuilder.Build();

bannerAd = new LevelPlayBannerAd(bannerAdUnitId, bannerConfig);

Destroy Banner Ad

To destroy a banner, call the
DestroyAd
method.
A destroyed banner can no longer be shown again. To display more ads, create a new
LevelPlayBannerAd
object.
bannerAd.DestroyAd();

Full Implementation Example of Banner Ads

Here is an example for creating and loading a banner ad using adaptive banner size.
public class BannerAdSample {
  private LevelPlayBannerAd bannerAd;
  void CreateBannerAd() {
    // Create ad configuration - optional
    var adConfig = new LevelPlayBannerAd.Config.Builder()
      .SetSize(LevelPlayAdSize.BANNER)
      .SetPlacementName("placementName")
      .SetPosition(LevelPlayBannerPosition.BottomCenter)
      .SetDisplayOnLoad(true)
      .SetRespectSafeArea(true)
      .Build();
        
    // Create banner instance
    bannerAd = new LevelPlayBannerAd("bannerAdUnitId", adConfig);
    // Subscribe BannerAd events
    bannerAd.OnAdLoaded += BannerOnAdLoadedEvent;
    bannerAd.OnAdLoadFailed += BannerOnAdLoadFailedEvent;
    bannerAd.OnAdDisplayed += BannerOnAdDisplayedEvent;
    bannerAd.OnAdDisplayFailed += BannerOnAdDisplayFailedEvent;
    bannerAd.OnAdClicked += BannerOnAdClickedEvent;
    bannerAd.OnAdCollapsed += BannerOnAdCollapsedEvent;
    bannerAd.OnAdLeftApplication += BannerOnAdLeftApplicationEvent;
    bannerAd.OnAdExpanded += BannerOnAdExpandedEvent;
  }
  public void LoadBannerAd() {
    //Load the banner ad 
    bannerAd.LoadAd();
  }
  public void ShowBannerAd() {
    //Show the banner ad, call this method only if you turned off the auto show when you created this banner instance.
    bannerAd.ShowAd();
  }
  public void HideBannerAd() {
    //Hide banner
    bannerAd.HideAd();
  }
  public void DestroyBannerAd() {
    //Destroy banner
    bannerAd.DestroyAd();
  }
  //Implement BannerAd Events
  public void BannerOnAdLoadedEvent(LevelPlayAdInfo adInfo) {}
  public void BannerOnAdLoadFailedEvent(LevelPlayAdError ironSourceError) {}
  public void BannerOnAdClickedEvent(LevelPlayAdInfo adInfo) {}
  public void BannerOnAdDisplayedEvent(LevelPlayAdInfo adInfo) {}
  public void BannerOnAdDisplayFailedEvent(LevelPlayAdInfo adInfo, LevelPlayAdError error){}
  public void BannerOnAdCollapsedEvent(LevelPlayAdInfo adInfo) {}
  public void BannerOnAdLeftApplicationEvent(LevelPlayAdInfo adInfo) {}
  public void BannerOnAdExpandedEvent(LevelPlayAdInfo adInfo) {}
}

LevelPlay Mediation Demo App

The Integration Demo application demonstrates how to integrate banner Ad Unit APIs in your app. Download Unity Demo Application You are now all set up to serve banners in your application. Verify your integration with our Integration Test Suite. It is possible to test your integration inside the editor. To learn how to preview mock ads in the Unity editor, see the Preview mock ads guide. What’s Next? Follow our integration guides to integrate additional Banner Ad networks or configure additional ad formats:

Banner integration for Unity • Unity Grow • Unity Docs