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.Note: the// Create the banner object and set the ad unit id LevelPlayBannerAd bannerAd = new LevelPlayBannerAd(bannerAdUnitId);
LevelPlayBannerAd
Config
If you do not require special settings, you can omit the// Use optional Config parameter LevelPlayBannerAd(string adUnitId, Config config = null)
Config
adUnitId
Attribute | Default |
---|---|
Size | Banner |
Position | BottomCenter |
displayOnLoad | true |
respectSafeArea | false (Android only) |
Config
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) |
---|---|---|
BANNER | Standard banner | 320 x 50 |
LARGE | Large banner | 320 x 90 |
MEDIUM_RECTANGLE | Medium Rectangular (MREC) | 300 x 250 |
Adaptive | Automatically renders ads to adjust size and orientation for mobile & tablets | Device width X recommended height |
- 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:
Specific banner size: This option allows you to set a specific banner size: BANNER, LARGE, MEDIUM_RECTANGLE.LevelPlayAdSize adSize = LevelPlayAdSize.CreateAdaptiveAdSize();
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
You can also position a Banner at a specific (x, y) coordinate on the screen by creating aLevelPlayBannerAd bannerAd = new LevelPlayBannerAd(adUnitId, LevelPlayAdSize.BANNER, LevelPlayBannerPosition.TopLeft);
LevelPlayBannerPosition
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 theLoadAd
// Set the placement name LevelPlayBannerAd bannerAd = new LevelPlayBannerAd("bannerAdUnitId", LevelPlayAdSize.BANNER, placementName:"placementName");
Implement the Banner Events
Listen to theLevelPlayBannerAd
- 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
TheLevelPlayAdInfo
Load Banner Ad
To load a banner ad useLoadAd
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.bannerAd.HideAd();// Pause refresh bannerAd.PauseAutoRefresh(); // Resume refresh bannerAd.ResumeAutoRefresh();
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, createLevelPlayBannerAd
respectSafeArea
Config
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 theDestroyAd
LevelPlayBannerAd
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) {} }