나. EventHandler

EventHandler 클래스는 포인트 조회나 포인트 인출과 같이 비동기로 결과를 받아야 하는 경우 또는 중간 전면광고에서 발생하는 이벤트를 처리하기 위해서 제공되는 클래스입니다.

EventHandler 클래스의 모습은 아래와 같습니다.

EventHandler 클래스

using UnityEngine;

using System.Collections;

namespace TnkAd {

public class EventHandler : MonoBehaviour {

// publishing state

public const int PUB_STAT_NO = 0; // not publishing yet

public const int PUB_STAT_YES = 1; // publising state

public const int PUB_STAT_TEST = 2; // testing state

// onClose(int type)

public const int CLOSE_SIMPLE = 0; // users simply closed ad view.

public const int CLOSE_CLICK = 1; // users clicked ad view.

public const int CLOSE_EXIT = 2; // users clicked exit app button.

// onFailure(int errCode)

public const int FAIL_NO_AD = -1; // no ad available

public const int FAIL_NO_IMAGE = -2; // ad image not available

public const int FAIL_TIMEOUT = -3; // ad not arrived in 5 secs.

public const int FAIL_CANCELED = -4; // ad frequency setting

public const int FAIL_SYSTEM = -9;

// Set 'Handler Name' in Unity Inspector

public string handlerName;

// ...

// TnkAdListener methods

public virtual void onClose (int type) {}

public virtual void onFailure (int errCode) {}

public virtual void onLoad() {}

public virtual void onShow() {}

// for Video Ad only

public virtual void onVideoCompleted(bool skipped) {}

// ServiceCallback method

public virtual void onReturnQueryPoint (int point) {}

public virtual void onReturnWithdrawPoints (int point) {}

public virtual void onReturnPurchaseItem(long curPoint, long seqId) {}

public virtual void onReturnQueryPublishState(int state) {}

}

}

EventHandler를 사용하기 위해서 다음과 같이 진행하세요.

  1. 우선 새로운 스크립트를 생성하시고 상위 클래스를 EventHandler를 변경합니다.

  2. 처리해야할 메소드를 override 하여 구현합니다. (EventHandler의 모든 메소드를 override 하실 필요는 없습니다. 처리해야햘 메소드만 구현하세요.)

  3. 구현된 클래스를 Unity의 Scene 화면의 GameObject 객체에 추가합니다. (기존의 GameObject에 추가하셔도 되고 새로운 GameObject를 생성하시어 추가하여도 상관없습니다.)

  4. Unity Inspector 화면에서 추가한 스크립트의 Handler Name 속성에 이름을 지정합니다. 여기에 지정된 이름이 TnkAd.Plugin의 API 호출시 사용됩니다.

1) EventHandler 스크립트 만들기

EventHandler 구현 예시

using UnityEngine;

using System.Collections;

public class MyTnkHandler : TnkAd.EventHandler {

public override void onReturnQueryPoint(int point) {

Debug.Log("##### onReturnQueryPoint " + point.ToString());

}

public override void onReturnPurchaseItem(long curPoint, long seqId) {

Debug.Log("##### onReturnPurchaseItem point = " + curPoint.ToString());

Debug.Log("##### onReturnPurchaseItem seqId = " + seqId.ToString());

}

}

2) GameObject에 추가하고 Handler Name 지정

3) 설정한 Handler Name 을 사용하여 Plugin API 호출

EventHandler 사용 예시

if (GUI.Button (new Rect (100, 400, 150, 80), "Query point")) {

Debug.Log("Query point");

// be sure that put handler object named 'testhandler' in your scene. (It should be named in Unity Inspector)

TnkAd.Plugin.Instance.queryPoint("testhandler");

}