Banner Ad object - cleveradssolutions/CAS-Unity GitHub Wiki

:zap: Before you start
Make sure you have correctly initialized Mediation Manager via Script or Unity Editor.

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 into a Unity app. In addition to code snippets and instructions, it also includes information about sizing banners properly.

Add an BannerAdObject to the scene

  1. Add an GameObject to your scene using GameObject > Create Empty in the Unity Editor.
  2. Add Component CleverAdsSolutions/Banner Ad Object in the inspector to new GameObject.
  3. Select Manager ID from settings asset for each runtime platform.
  4. Select Ad position
  5. Select Ad size
  6. Invoke gameObject.SetActive(true) method to show the banner ad.

image

[!NOTE]
An BannerAdObject does not have any rendered components. Therefore, you can place the GameObject anywhere in the scene.

Load Ad callbacks

  • On Ad Loaded
    The event is invoked when the banner ad has finished loading.
  • On Ad Failed To Load
    The event is invoked when the banner ad fails to load. The Message parameter describes the type of failure that occurred.

Content callbacks

  • On Ad Shown
    The event is invoked when the banner ad is displayed.
  • On Ad Clicked
    The event is invoked when the user clicks on the banner ad.
  • On Ad Hidden
    The event is invoked when the banner ad is hidden from screen.

Unity Events sample

You can implement functions that correspond to ad callbacks. For example, if you want to handle when a banner ad fails to load:

  1. Create a function compatible with the ad callback in custom component.
public void OnBannerAdFailedToLoad(string reason) {
    Debug.Log("Banner ad failed to load: " + reason);
}
  1. Attach the script which contains the above function to any GameObject in the scene.
  2. Click the + button, then drag & drop the GameObject that you've attached the script to.
  3. Select the function that you want to link to the ad callback. For the parameterized ad callbacks, select the function to accept the dynamic variable so you can get the parameter value from the SDK.

Use BannerAdObject from script

Get the active banner ad instance from the script:

BannerAdObject activeBanner = BannerAdObject.Instance;

Show the banner ad on screen:

activeBanner.gameObject.SetActive(true);

Hide the banner ad from screen:

activeBanner.gameObject.SetActive(false);

Set the banner Ad position on screen:

activeBanner.SetAdPosition(AdPosition.TopCenter);

Set the banner ad size:

activeBanner.SetAdSize(AdSize.Adaptive);

Calculate the actual size of an ad banner in pixels on the screen:

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

🔗 Done! What’s Next?