Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Native ads V2

fan-c-yu edited this page Mar 5, 2021 · 7 revisions

Preparation

If you have not created ad spaces or downloaded SDK, please refer to the link below.

SDK Implementation

If you have not imported Unity package into project, please follow the link below.


How to show native ads

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.

Implementation procedure

  1. Acquire INativeAdClient instance by NativeAdClientFactory#NewClient method.(Call NewClient method from Unity’s main thread)
  2. Load ads by INativeAdClient#LoadNativeAd method. Acquire each field of native ads from INativeAd instance by call-back. Display specified object.
  3. 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);
            }
        });
    }
}

About IF in INativeAdClient

You can use the following IF from app side.

public void LoadNativeAd (System.Action<INativeAd, int, string> callback)

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.

public void EnableAutoReload (double interval, System.Action<INativeAd, int, string> 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.

public void DisableAutoReload ()

Stop auto ad reload.

Sample Code: Load NendAdNativeView ads which have not acquired ads.
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.
    }
}

About IF in INativeAd

You can use the following IF from app side.

public string ShortText { get; }

Acquire ad header.

public string LongText { get; }

Acquire ad text.

public string PromotionUrl { get; }

Acquire display promotion URL.

public string PromotionName { get; }

Acquire display promotion name.

public string ActionButtonText { get; }

Acquire action button(e.g. installs).

public string AdImageUrl { get; }

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.

public string LogoImageUrl { get; }

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.

public string GetAdvertisingExplicitlyText (AdvertisingExplicitly advertisingExplicitly)

Acquire text for Advertising Explicitly. Please refer here for the detail of argument.

public System.Collections.IEnumerator LoadAdImage(System.Action<UnityEngine.Texture2D> callback)

Acquire Texture2D of ad image.If ad type has no ad image, null will be returned.

public System.Collections.IEnumerator LoadLogoImage(System.Action<UnityEngine.Texture2D> callback)

Acquire Texture2D of logo image.If ad type has no logo image, null will be returned.

public void Activate (UnityEngine.GameObject adGameObject, UnityEngine.GameObject prGameObject)

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.

public void Into (NendUnityPlugin.AD.Native.NendAdNativeView adView)

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);
    }
});
public event System.EventHandler AdClicked

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.");
        };
    }
});

Verification

Clone this wiki locally