3) Unity

Before proceeding this chapter, be sure that you already finished SDK settings. If not, check 5) Unity Settings first.

[Incentive Interstitial Ad]

To display incentive interstitial ads in your app, simply write down two lines of code as follows.

Incentive Ad Sample

using UnityEngine;
using System.Collections;

public class TnkUITest : MonoBehaviour {

    void Start ()
    {
    }
    
    void Update ()
    {
    }
    
    void OnGUI ()
    {
        if (GUI.Button (new Rect (10010015080), "Interstitial Ad")) {
            Debug.Log("interstitial Ad");
            TnkAd.Plugin.Instance.prepareInterstitialAdForPPI();
            TnkAd.Plugin.Instance.showInterstitialAd();
        }
    }
}



[Non-Incentive Intestitial Ad]

To display non-incentive interstitial ads in your app, simply write down two lines of code as follows.

Non-Incentive Ad Sample

using UnityEngine;
using System.Collections;

public class TnkUITest : MonoBehaviour {

    void Start ()
    {
    }
    
    void Update ()
    {
    }
    
    void OnGUI ()
    {
        if (GUI.Button (new Rect (10010015080), "Interstitial Ad")) {
            Debug.Log("interstitial Ad");
            TnkAd.Plugin.Instance.prepareInterstitialAdForCPC();
            TnkAd.Plugin.Instance.showInterstitialAd();
        }
    }
}

 
[Interstitial Ad APIs]

API specs for prepareInterstitialAd() and showInterstitialAd().

- TnkAd.Plugin - prepareInterstitialAd()

Method
  - void prepareInterstitialAd(string logicName)
  - void prepareInterstitialAd(string logicName, string handlerName)

Description
   It loads a single interstitial Ad from server and cache it in memory.

Parameters
 파라메터 명칭 내용
 logicNameEnter TnkSession.PPI for incentive ads or TnkSession.CPC for non-incentive ads or any other string for Display Logic. See 5) Display Logic for details.
handlerName Name of EventHandler to which events are sent.

- TnkAd.Plugin - showInterstitialAd()

Method
  - void showInterstitialAd()

Description
  It displays an interstitial ad which is loaded by prepareInterstitialAd() call before. 

[EventHandler]

To catch events that occurs when an interstitial ad is loaded or when users close interstitial ads, use EventHandler objects.
EventHandler is for getting return values from async invocations or getting events related to interstitial popup view.
See B. EventHandler for details about EventHandler.

Following sample code shows how developers can control displaying interstitial ads using EventHandler.

EventHandler Sample

using UnityEngine;
using System.Collections;

public class MyTnkHandler : TnkAd.EventHandler {

    public bool isOkToShow = true;

    public override void onClose (int type) {
        Debug.Log ("##### TnkAd.Listener onClose " + type.ToString ());
    }
    
    public override void onFailure(int errCode) {
        Debug.Log ("##### TnkAd.Listener onFailure " + errCode.ToString ());
    }
    
    public override void onLoad() {
        Debug.Log ("##### TnkAd.Listener onLoad ");
        if (isOkToShow) {
            TnkAd.Plugin.Instance.showInterstitialAd ();
        } else {
            // not showing
        }
    }
    
    public override void onShow() {
        Debug.Log ("##### TnkAd.Listener onShow ");
    }
}


Interstitial Ad with EventHandler

using UnityEngine;
using System.Collections;

public class TnkUITest : MonoBehaviour {

    void Start ()
    {
    }
    
    void Update ()
    {
    }
    
    void OnGUI ()
    {
        if (GUI.Button (new Rect (10020015080), "Ad Listener")) {
            Debug.Log("interstitial Ad for Listener");
            // be sure that put handler object named 'testhandlerin your scene. (It should be named in Unity Inspector)
            TnkAd.Plugin.Instance.prepareInterstitialAd("notice_start""testhandler"); 
        }
    }

}

[Back key (Android only)]

Unity doesn't send back-key event to java side. So you have to add the following code to a script of yours that is attached to an active MonoBehaviour class. You can replace Application.Quit() with anything you want.

Text Box

using UnityEngine;
using System.Collections;

public class TnkUITest : MonoBehaviour {

    void Start ()
    {
    }
    
    void Update ()
    {
        if (Application.platform == RuntimePlatform.Android) {
            if (Input.GetKeyUp(KeyCode.Escape)) {
                if (TnkAd.Plugin.Instance.isInterstitialAdVisible()) {
                    TnkAd.Plugin.Instance.onBackPressed();
                }
                else {
                    // put your logic for back-key pressed
                    Application.Quit();
                }
            }
        }
    }
    // ... 
}

Comments