-
Notifications
You must be signed in to change notification settings - Fork 5
Native ads V2
If you have not created ad spaces or downloaded SDK, please refer to the link below.
If you have not imported Unity package into project, please follow the link below.
From v2.1.0 Unity plug-in, each field of native ads can be acquired from app side. Because of this, you can display native ads on not only uGUI but also other objects.
Please display native ads, referring to this for native ads guideline.
- Acquire
INativeAdClient
instance byNativeAdClientFactory#NewClient
method.(CallNewClient
method from Unity’s main thread) - Load ads by
INativeAdClient#LoadNativeAd
method. Acquire each field of native ads fromINativeAd
instance by call-back. Display specified object. - Register impression count and ad click processing by
INativeAd#Activate
method.
using System;
using UnityEngine;
using NendUnityPlugin.AD.Native;
...
public class NativeAdObject : MonoBehaviour {
private INativeAdClient m_AdClient;
// TextMesh and Sprite are used.
public TextMesh prText;
public TextMesh shortText;
public SpriteRenderer adImage;
...
void Start () {
...
#if UNITY_EDITOR
// In case of UnityEditor, you can check ad display by specifying ad space type and setting test ads.
m_AdClient = NativeAdClientFactory.NewClient (NativeAdClientFactory.NativeAdType.SmallSquare);
#elif UNITY_IOS
m_AdClient = NativeAdClientFactory.NewClient (ios_spotid, "ios_apikey");
#elif UNITY_ANDROID
m_AdClient = NativeAdClientFactory.NewClient (android_spotid, "android_apikey");
#endif
m_AdClient.LoadNativeAd ((INativeAd ad, int code, string message) => {
if (null != ad) {
// Acquire text for Advertising Explicitly
prText.text = ad.GetAdvertisingExplicitlyText (AdvertisingExplicitly.AD);
// Acquire ad header
shortText.text = ad.ShortText;
// Download ad image Texture
StartCoroutine (ad.LoadAdImage ((Texture2D texture) => {
adImage.sprite = Sprite.Create (texture, new Rect (0, 0, texture.width, texture.height), new Vector2 (0.5f, 0.5f));
}));
// Register impression count and ad click processing.
ad.Activate (gameObject, prText.gameObject);
} else {
Debug.LogFormat ("Failed to load ad. code = {0}, message = {1}", code, message);
}
});
}
}
You can use the following IF from app side.
Load native ads.
If ad load fails, error code is passed to the 2nd argument of callback and error message is passed to the 3rd argument of callback.
Reload ads automatically by specified interval (millisecond).
Reload interval has to be 30 seconds or more.
The content of callback is same as LoadNativeAd
.
Stop auto ad reload.
using UnityEngine;
using NendUnityPlugin.AD.Native;
...
public class YourScene : MonoBehaviour {
private INativeAdClient m_Client = ...;
void OnApplicationPause (bool pauseStatus) {
if (pauseStatus) {
m_Client.DisableAutoReload ();
} else {
// e.g. Reload every 1 minute
m_Client.EnableAutoReload (60000.0, ReloadCallback);
}
}
private void ReloadCallback (INativeAd ad, int code, string message) {
// do something.
}
}
You can use the following IF from app side.
Acquire ad header.
Acquire ad text.
Acquire display promotion URL.
Acquire display promotion name.
Acquire action button(e.g. installs).
Acquire ad image URL.
You can use this function if you want to download images from app side.
If ad type has no ad image, empty text will be returned.
Acquire logo image URL.
You can use this function if you want to download images from app side.
If ad type has no logo image, empty text will be returned.
Acquire text for Advertising Explicitly. Please refer here for the detail of argument.
Acquire Texture2D
of ad image.If ad type has no ad image, null
will be returned.
Acquire Texture2D
of logo image.If ad type has no logo image, null
will be returned.
Register impression count and ad click processing.
GameObject
of the first argument is measurement object of ad clicks and impressions.
GameObject
of the second argument has to be GameObject
which dislpalys Advertising Explicitly and it has to be moved to opt-out page when clicked.
In case non uGUI GameObject
is set, each GameObject
has to be attached to Collider
.
Draw ad information on NendAdNativeView.
using System;
using NendUnityPlugin.AD.Native;
...
INativeAdClient client = ...;
client.LoadNativeAd ((INativeAd ad, int code, string message) => {
if (null != ad) {
// Acquire instance from GameObjectwith NendAdNativeView
var adView = gameObject.GetComponet <NendAdNativeView> ();
ad.Into (adView);
}
});
Register events which are called when ad is alicked.
using System;
using NendUnityPlugin.AD.Native;
...
INativeAdClient client = ...;
client.LoadNativeAd ((INativeAd ad, int code, string message) => {
if (null != ad) {
ad.AdClicked += (sender, e) => {
UnityEngine.Debug.Log ("Ad Clicked.");
};
}
});