Banner Ads - cleveradssolutions/CAS-Unity GitHub Wiki

:zap: Before you start
Make sure you have correctly Initialize SDK.

Implementation by UnityEditor | Script C#


Banner ads 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.

This guide shows you how to integrate banner ads from CAS into a Unity app. In addition to code snippets and instructions, it also includes information about sizing banners properly.

Enable Banner Ads for your game in Assets > CleverAdsSolutions > Settings menu.

For easier ads integration using the Unity Editor, try the new BannerAdObject.

Ad size

Size in dp (WxH) Description Availability AdSize
320x50 Standard Banner Phones and Tablets Banner
728x90 IAB Leaderboard Tablets Leaderboard
300x250 IAB Medium Rectangle Phones and Tablets MediumRectangle
Adaptive Adaptive banner Phones and Tablets AdaptiveBanner
320x50 or 728x90 Smart Banner Phones and Tablets SmartBanner

❕ Typically, Smart Banners on phones have a Banner size. Or on tablets a Leaderboard size.

Get the ad view interface for specific AdSize:

IAdView adView = manager.GetAdView( AdSize.Banner );

[!NOTE]
If a view for specific size has already been created then a reference to it will be returned without creating a new one.

Adaptive banners

Adaptive banners are the next generation of responsive ads, maximizing performance by optimizing ad size for each device.
To pick the best ad size, adaptive banners use fixed aspect ratios instead of fixed heights. This results in banner ads that occupy a more consistent portion of the screen across devices and provide opportunities for improved performance.

  • The height of adaptive banners cannot be less than 50 dp and more than 250 dp.
  • The width of adaptive banners cannot be less than 300 dp.
  • The adaptive banners use fixed aspect ratios instead of fixed heights.

[!NOTE]
After changing the screen orientation, the width of the banner changes automatically to the current screen width. This will destroy the current ad content and load a new one.

Load an Ad

:star: By default, the auto-load ads mode is used and a load method does not need to be called.
If you use LoadingManagerMode.Manual then please call load after manager.GetAdView().
Also you can use the load for cancel current impression and load next ad.

adView.Load();

Ad Availability

You can ask for the ad availability directly by calling the following function:

bool bannerLoaded = adView.isReady;

[!IMPORTANT]
We don’t recommend waiting for isReady to change in an Update or Coroutine. This property call the native SDK and can affect the performance of your game. Subscribe to the OnLoaded event instead.

Display the Ad

The newly created AdView has an inactive state.
When you are ready to show the ad on the screen, simply call the following method:

adView.SetActive(true);

When you need to hide AdView from the user, just deactivate it:

adView.SetActive(false);

[!NOTE]
You can change the banner's activity even if it is not ready to be shown yet. When the ad is ready, banner will shown automatically.

Ad events

To further customize the behavior of your ad, you can hook into a number of events in the ad's lifecycle: loading, opening, closing, and so on. Listen for these events by registering a delegate for the appropriate event, as shown below.

// Called when the ad loaded and ready to present.
adView.OnLoaded += (view) => Debug.Log("Banner Ad Loaded");
// Сalled when an error occurred with the ad.
adView.OnFailed += (view, error) => Debug.Log("Banner Ad Failed with error: " + error.ToString());
// Called when the user clicks on the Ad.
adView.OnClicked += (view) => Debug.Log("Banner Ad clicked");

[!NOTE]
Read more about event OnImpression with Impression Level Data and AdMetaData.

Ad position

The position where the banner ad should be placed. The CAS.AdPosition enum lists the valid ad position values.
By default, the banner ad is centered at the bottom.
Changing the position of a banner ad does not destroy the current banner or load a new one.

Change the banner position on screen using the following method:

adView.position = AdPosition.TopCenter;

Use the SetPosition(x, y) for greater control over where a AdView is placed on screen than what's offered by AdPosition values.

adView.SetPosition(xOffsetInDP, yOffsetInDP);

The top-left corner of the AdView will be positioned at the x and y values passed to the method, where the origin is the top-left of the screen.
The coordinates on the screen are determined not in pixels, but in Density-independent Pixels(DP).

[!NOTE]
When the ad view is positioned using coordinates, the adView.position == AdPosition.TopLeft.

Current rect in pixels

You can calculate the actual rect of the Ad Banner in pixels on the screen.

Rect rect = adView.rectInPixels;
Vector2 adSizePixels = rect.size;
Vector2 adPositionInPixels = rect.position; 

This value is buffered after calculation, feel free to check ad view rect each frame.
The position on the screen is calculated with the addition of indents for the cutouts.

Ad Refresh rate

An ad unit’s automatic refresh rate (in seconds) determines how often a new ad request is generated for that ad unit.
We recommended using refresh rate 30 seconds. However, you can choose any value you want longer than 10 seconds.

[!NOTE]
Ad requests should not be made when the device screen is turned off.

Change the banner automatic refresh rate using the following method:

adView.refreshInterval = refreshIntervalInSeconds;

Or disable automatic refresh ads with method:

adView.DisableRefresh();

Free ad memory

If you no longer need the AdView with specific size, please call IDisposable.Dispose() to free memory.

adView.Dispose();

After calling Dispose(), you can use GetAdView() method to create a new view.

[!TIP]


🔗 Done! What’s Next?