Adaptive banners
Types of adaptive banners
The Yandex Mobile Ads SDK has three types of adaptive banners:
|
Banner |
Description |
When to use |
|
Sticky banner |
A banner that is pinned to the screen edge and displayed on top of other elements. Banners of this type are unaffected by scrolling. |
When you want a banner to “stick”, which means to remain in its place and be shown on top of content even if a user scrolls the screen. |
|
Inline banner |
A banner that is embedded in your app (for example, in content or a list) as if it's a UI element. |
When the banner should be part of the page content, appearing in a news feed, between posts, or as a list item. |
|
Fixed banner |
A banner that is fixed in a certain place or used for spaces with fixed height. |
When you need to place a banner in a static location, for example, under a toolbar or at the bottom of the screen. |
Banners of all types automatically refresh creatives every 60 seconds.
Tip
To set a custom auto‑refresh period, contact your personal manager.
Code-wise, these banners only differ in how you set their sizes. Examples:
BannerAdSize.StickySize(int width)
BannerAdSize.InlineSize(int width, int maxHeight)
BannerAdSize.Fixed(int width, int height)
Note
Examples showing how all the format types work are available in the demo project.
|
Entity |
Description |
|
|
The banner height:
All banner sizes must be specified in dp. |
|
|
Use:
|
Example of creating an adaptive banner
using UnityEngine;
using UnityEngine.Events;
using YandexMobileAds;
using YandexMobileAds.Base;
using System;
[AddComponentMenu("Yandex Ads/Banner Component")]
public class BannerComponent : MonoBehaviour
{
public enum BannerType
{
Inline,
Sticky,
Fixed
}
[Header("Banner Settings")]
public string bannerName = "Default Banner";
public BannerType bannerType = BannerType.Inline;
[Header("Ad Unit IDs")]
[Tooltip("Ad Unit ID for Android")]
public string adUnitIdAndroid = "demo-banner-yandex";
[Tooltip("Ad Unit ID for iOS")]
public string adUnitIdiOS = "demo-banner-yandex";
[Header("Banner Configuration")]
public AdPosition bannerPosition = AdPosition.BottomCenter;
public bool useScreenWidth = false;
public int manualWidth = 320;
public int bannerHeight = 50;
public bool showAfterLoading = true;
private Banner banner;
[System.Serializable]
public class AdEvent : UnityEvent { }
[System.Serializable]
public class AdEventString : UnityEvent<string> { }
[System.Serializable]
public class AdEventWithBanner : UnityEvent<Banner> { }
[Header("Load Callbacks")]
public AdEventWithBanner OnAdLoaded;
public AdEventString OnAdFailedToLoad;
[Header("Interaction Callbacks")]
public AdEventWithBanner OnAdClicked;
public AdEventWithBanner OnLeftApplication;
public AdEventWithBanner OnReturnedToApplication;
public AdEventString OnImpression;
[Header("Configure Callbacks")]
public AdEventWithBanner OnAdConfigured;
public AdEventString OnAdConfigurationFailed;
private string currentAdUnitId
{
get
{
#if UNITY_IOS
return adUnitIdiOS;
#elif UNITY_ANDROID
return adUnitIdAndroid;
#else
Debug.LogWarning("Unsupported platform for Yandex Ads. Using IOS Ad Unit ID by default.");
return adUnitIdAndroid;
#endif
}
}
private void Start()
{
if (!string.IsNullOrEmpty(currentAdUnitId))
{
this.load();
}
else
{
Debug.LogError("Banner configuration failed. Ad Unit ID is missing.");
}
}
private void ConfigureBanner()
{
BannerAdSize adSize = bannerType == BannerType.Sticky
? BannerAdSize.StickySize(useScreenWidth ? ScreenUtils.ConvertPixelsToDp((int)Screen.safeArea.width) : manualWidth)
: BannerAdSize.InlineSize(useScreenWidth ? ScreenUtils.ConvertPixelsToDp((int)Screen.safeArea.width) : manualWidth, bannerHeight);
banner = new Banner(currentAdUnitId, adSize, bannerPosition);
banner.OnAdLoaded += HandleAdLoaded;
banner.OnAdFailedToLoad += HandleAdFailedToLoad;
banner.OnAdClicked += HandleAdClicked;
banner.OnLeftApplication += HandleLeftApplication;
banner.OnReturnedToApplication += HandleReturnedToApplication;
banner.OnImpression += HandleImpression;
}
private void load()
{
ConfigureBanner();
banner.LoadAd(new AdRequest.Builder().Build());
}
public void loadAd()
{
this.load();
}
public void OnDestroy()
{
banner.OnAdLoaded -= HandleAdLoaded;
banner.OnAdFailedToLoad -= HandleAdFailedToLoad;
banner.OnAdClicked -= HandleAdClicked;
banner.OnLeftApplication -= HandleLeftApplication;
banner.OnReturnedToApplication -= HandleReturnedToApplication;
banner.OnImpression -= HandleImpression;
banner.Destroy();
banner = null;
}
#region Event Handlers
private void HandleAdLoaded(object sender, EventArgs args)
{
OnAdLoaded?.Invoke(banner);
if (showAfterLoading)
{
banner.Show();
}
}
private void HandleAdFailedToLoad(object sender, AdFailureEventArgs args) => OnAdFailedToLoad?.Invoke(args.Message);
private void HandleAdClicked(object sender, EventArgs args) => OnAdClicked?.Invoke(banner);
private void HandleLeftApplication(object sender, EventArgs args) => OnLeftApplication?.Invoke(banner);
private void HandleReturnedToApplication(object sender, EventArgs args) => OnReturnedToApplication?.Invoke(banner);
private void HandleImpression(object sender, ImpressionData impressionData) =>
OnImpression?.Invoke(impressionData?.rawData ?? string.Empty);
#endregion
}
Checking integration
Build and run your project. You can check if the integration is successful by searching the YandexAds keyword in Logcat in Android Studio:
[Integration] Ad type App Open was integrated successfully