Applying Native Ad Response
This page explains how to make use of INativeAd.
On how to build the request and load the ad see Building Native Ad Request.
Tip
You can find a functional code in "Native" sample.
Where we left off
As was mentioned on the previous page, we have 3 (still relevant) i-vars.
[SerializeField] private TMP_Text? text;
private INativeAdLoader? _nativeAdLoader;
private INativeAd? _nativeAd;
We will use text
to dump all the data from our INativeAd -- for illustration purposes.
In the real app you will populate your own GUI (or 2D/3D) components.
_nativeAd
still has the default value (null).
_nativeAdLoader
still has the loader that was used to start loading the ad.
_nativeAdLoader.LoadAd(OnAdLoadResult);
Logging helper
LogToUI
method is a simple wrapper to add messages both to UI and debug console:
private void LogToUI(string message, LogType logType = LogType.Log)
{
if (text != null)
{
text.text += "\n" + message;
}
Debug.LogFormat(logType, LogOption.None, gameObject, "[{0:O}] {1}", DateTime.Now, message);
}
Ad Load Callback
First, check if we did get the actual ad.
_nativeAdLoader = null;
if (nativeAd is null)
{
LogToUI($"Failed to load ad: {status?.Message}", LogType.Error);
return;
}
Save a reference to the ad.
_nativeAd = nativeAd;
Note
INativeAd extends IDisposable. Calling Dispose() releases native objects and stops impression tracking.
You should dispose of native ad once done, but preserve a reference until that point.
Event handling
Subscribing to events
SubscribeToEvents(nativeAd);
private void SubscribeToEvents(INativeAd nativeAd)
{
nativeAd.OnAdClicked += OnAdClicked;
nativeAd.OnAdImpression += OnAdImpression;
nativeAd.OnAdExpired += OnAdExpired;
}
Expiration handling
private void OnAdExpired()
{
LogToUI("Ad Expired.");
using (_nativeAd)
{
if (_nativeAd is not null)
{
UnsubscribeFromEvents(_nativeAd);
_nativeAd.Dispose();
_nativeAd = null;
}
}
}
Unsubscribing
private void UnsubscribeFromEvents(INativeAd nativeAd)
{
nativeAd.OnAdClicked -= OnAdClicked;
nativeAd.OnAdImpression -= OnAdImpression;
nativeAd.OnAdExpired -= OnAdExpired;
}
Extracting all the things
private void UnsubscribeFromEvents(INativeAd nativeAd)
{
nativeAd.OnAdClicked -= OnAdClicked;
nativeAd.OnAdImpression -= OnAdImpression;
nativeAd.OnAdExpired -= OnAdExpired;
}
Iteration over typed asset collections
Use foreach to iterate over Titles:
foreach (var nextTitle in nativeAd.Titles)
{
}
Note
ITitleContent, IImageContent and IDataContent implement IDisposable.
Calling Dispose() allows the engine to free native resources sooner on some platforms.
See "Best practice" / "Garbage collection" section of Call Java and Kotlin plug-in code from C# scripts for details.
Since we no longer need titles, once we extract the strings, we will use using as recommended.
foreach (var nextTitle in nativeAd.Titles)
{
using (nextTitle)
{
}
}
Similar loops can be made for Images / DataObjects.
Inside the loop(s) you can access all the properties exposed by (respective) asset type(s):
You might want to use a switch over ImageContentType / DataContentType to determine which of your components (on scene) should handle curent ad asset.
Convenience accessors
Few direct access string properties can be helpful to avoid filtering through all the assets (if you know what you are looking for).
Enabling impression tracking
Call RegisterView(GameObject, IEnumerable<GameObject>?).
As a container
pass the most relevant ancestor (Transform-wise) of ad elements.
As a clickableObjects
-- GameObjects with attached colliders (Collider / Collider2D) or Selectable components.
nativeAd.RegisterView(gameObject, null);
Disposing of native ad
UnsubscribeFromEvents(_nativeAd);
_nativeAd.Dispose();
_nativeAd = null;
Used in Expiration handling but may also be needed when GameObject gets destroyed.