-
Notifications
You must be signed in to change notification settings - Fork 4
Banner Ad object
⚡ Before you start
Make sure you have correctly initialized Mediation Manager via Script or Unity Editor.
Implementation by UnityEditor | Script C#
- Add an BannerAdObject to the scene
- Load Ad callbacks
- Content callbacks
- Unity Events sample
- Use BannerAdObject from script
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 GameObject to your scene using
GameObject > Create Empty
in the Unity Editor. - Add Component
CleverAdsSolutions/Banner Ad Object
in the inspector to new GameObject. - Select Manager ID from settings asset for each runtime platform.
- Select Ad position
- Select Ad size
- Invoke
gameObject.SetActive(true)
method to show the banner ad.
Note
An BannerAdObject does not have any rendered components. Therefore, you can place the GameObject anywhere in the scene.
-
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.
-
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.
You can implement functions that correspond to ad callbacks. For example, if you want to handle when a banner ad fails to load:
- Create a function compatible with the ad callback in custom component.
public void OnBannerAdFailedToLoad(string reason) {
Debug.Log("Banner ad failed to load: " + reason);
}
- Attach the script which contains the above function to any GameObject in the scene.
- Click the + button, then drag & drop the GameObject that you've attached the script to.
- 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.
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?
- Follow our integration guides and implement our Ad Units:
- Read more about Impression level data.
- Project Setup
- Configuring SDK
- Include Android
- Include iOS
- Additional mediation steps
- App-ads.txt🔗