Adaptive banners

Warning.

This is an archived version of the documentation. Actual documentation for all platforms can be found here.

Adaptive banners are banners that fit seamlessly into user-defined unit sizes. Depending on how an adaptive banner is integrated, the optimal height is determined for the given width or the specified size of ad placement is used.

Note.

You can read about creating an ad unit for an adaptive banner in the Yandex Advertising Network Help.

  1. Types of adaptive banners
  2. Adding Banner to the project
  3. Clearing ads
  4. Example of working with adaptive banners

Types of adaptive banners

Banner with a set width

Features:

  1. An alternative to 320x50 banners (when determining the banner height, the 320x50 aspect ratio is maintained).
  2. The banner's position is fixed at the top or bottom of the screen (configured in the app).
  3. The given banner width is used instead of the device screen width. This lets you take into account the display's features.
  4. The width of an adaptive banner is set using the AdSize.StickySize(int width) method.

Examples of displaying adaptive banners:

Banner with a set width and height

Features:

  1. An adaptive banner fills up the entire unit using the given width and height.
  2. The width and height of an adaptive banner is set using the AdSize.FlexibleSize(int width, int height) method.

Examples of displaying adaptive banners:

Adding Banner to the project

  1. To display a banner in your app, create a Banner object in the script (in C#) that is attached to the GameObject.
    ...
    using YandexMobileAds;
    using YandexMobileAds.Base;
    ...
    
    public class YandexMobileAdsBannerDemoScript : MonoBehaviour
    {
        private Banner banner;
        ...
        private void RequestBanner()
        {
            string adUnitId = "demo-banner-yandex";
            banner = ...
        }
        ...
    }
    
  2. Specify the banner size and initialize the banner in the app. Use the ScreenUtils.ConvertPixelsToDp method to convert the width in pixels to density-independent pixels.

    Banner with a set width
    AdSize adSize = AdSize.StickySize(width);
    banner = new Banner(adUnitId, adSize, AdPosition.BottomCenter);
    Copied to clipboard
    Banner with a set width and height
    AdSize adSize = AdSize.FlexibleSize();
    banner = new Banner(adUnitId, AdSize.BANNER_320x50, AdPosition.BottomCenter);
    Copied to clipboard

Loading ads

Once you've created and set up the Banner class object, you need to load your ad. To load an ad, use the LoadAd method, accepting the AdRequest object as a parameter.

...
private void RequestBanner()
{
    ...
    AdRequest request = new AdRequest.Builder().Build();

    banner.LoadAd(request);
    ...
}
...
Copied to clipboard

Banner ad events

To track events that occur in banner ads, register a delegate for the appropriate EventHandler, as shown below:

...
private void RequestBanner()
{
    ...
        banner.OnAdLoaded += HandleAdLoaded;
        banner.OnAdFailedToLoad += HandleAdFailedToLoad;
        banner.OnReturnedToApplication += HandleReturnedToApplication;
        banner.OnLeftApplication += HandleLeftApplication;
        banner.OnAdClicked += HandleAdClicked;
        banner.OnImpression += HandleImpression;
    ...
}

public void HandleAdLoaded(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleAdLoaded event received");
    banner.Show();
}

public void HandleAdFailedToLoad(object sender, AdFailureEventArgs args)
{
    MonoBehaviour.print("HandleAdFailedToLoad event received with message: " + args.Message);
}

public void HandleLeftApplication(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleLeftApplication event received");
}

public void HandleReturnedToApplication(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleReturnedToApplication event received");
}

public void HandleAdLeftApplication(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleAdLeftApplication event received");
}

public void HandleAdClicked(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleAdClicked event received");
}

public void HandleImpression(object sender, ImpressionData impressionData)
{
    var data = impressionData == null ? "null" : impressionData.rawData;
    MonoBehaviour.print("HandleImpression event received with data: " + data);
}
Copied to clipboard

Clearing ads

When an ad object is no longer needed, you can delete it. To do this, call the Destroy method:

banner.Destroy();
Copied to clipboard

Example of working with adaptive banners

The following code demonstrates creating, configuring, and loading a Banner object:

...

public class YandexMobileAdsBannerDemoScript : MonoBehaviour
{
    private Banner banner;
    ...
    private void RequestBanner()
    {
        string adUnitId = "demo-banner-yandex";
        int screenWidth = (int)Screen.safeArea.width;
        int width = ScreenUtils.ConvertPixelsToDp(screenWidth);
        AdSize adSize = AdSize.StickySize(width);
        banner = new Banner(adUnitId, adSize, AdPosition.BottomCenter);
        AdRequest request = new AdRequest.Builder().Build();
        banner.LoadAd(request);
    }
    ...
}