nirasan's tech blog

趣味や仕事の覚え書きです。Linux, Perl, PHP, Ruby, Javascript, Android, Cocos2d-x, Unity などに興味があります。

Unity Ads 導入メモ

やったこと

  • Unity Ads のサイトにアカウント登録
  • Unity Ads でアプリの登録とID取得
  • Asset Store から Unity Ads のインポート
  • 広告の呼び出しスクリプトの作成と実行

スクリプト

AdsManager
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;

public class AdsManager : MonoBehaviour {

	void Awake() {
		if (Advertisement.isSupported) {
			Advertisement.allowPrecache = true;
			Advertisement.Initialize (Config.unityAdsId, true);

		} else {
			Debug.Log("Platform not supported");
		}
	}

	public void Show() {
		Advertisement.Show(null, new ShowOptions {
			pause = true,
			resultCallback = result => {
				Debug.Log("広告閲覧終了。広告閲覧報酬を付与するならここで。");
				Debug.Log(result.ToString());
			}
		});
	}
}
Config
using UnityEngine;
using System.Collections;

public class Config {

	#if UNITY_IPHONE
	public static string unityAdsId = "iOSアプリのID";
	#elif UNITY_ANDROID
	public static string unityAdsId = "AndroidアプリのID";
	#endif
}
広告表示
	GameObject.FindObjectOfType<AdsManager>().Show();