Skip to content

Instantly share code, notes, and snippets.

@matsuyoro
Created March 16, 2016 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsuyoro/2827f40c1f873c005ff7 to your computer and use it in GitHub Desktop.
Save matsuyoro/2827f40c1f873c005ff7 to your computer and use it in GitHub Desktop.
Unity Ads と Ad Colonyの動画SDKをUnity(C#)で一元管理したメモ(SDKだらけ〜〜w ref: http://qiita.com/matsuyoro/items/0959ba21a001d2dd7eca
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// unity ads
using UnityEngine.Advertisements;
public class GameSceneSingleton {
private static GameSceneSingleton mInstance;
private GameSceneSingleton () { // Private Constructor
}
public static GameSceneSingleton Instance {
get {
if( mInstance == null ) mInstance = new GameSceneSingleton();
return mInstance;
}
}
// unity ads 初期化
public void initUnityAds()
{
//プラットフォームが対応しているか判定
if (Advertisement.isSupported)
{
Advertisement.allowPrecache = true;
// ここにwifiかどうかや、1日何回まで再生等、通信トラフィックの制御をいれた方がいいかも(初期化時に通信発生
#if UNITY_IOS
Advertisement.Initialize(Constant.SDK_UNITY_ADS_IOS_GAME_ID);
#elif UNITY_ANDROID
Advertisement.Initialize (Constant.SDK_UNITY_ADS_IOS_GAME_ID);
#endif
}
else
{
Debug.Log("プラットフォームに対応していません。");
}
}
// unity adsが視聴可能か
public bool UnityAdsCanPlay()
{
//プラットフォームが対応しているかつ準備が完了している時だけtrueを返す
return Advertisement.isSupported && Advertisement.isReady();
}
//
public void UnityAdsPlay()
{
if (Advertisement.isReady())
{
// 準備OK
//callback用にクラスを定義
ShowOptions showoptions = new ShowOptions();
showoptions.pause = true;
showoptions.resultCallback = resAds; //コールバック先メソッド
//広告呼び出し
Advertisement.Show(null, showoptions);
}
else
{
// 準備失敗
}
}
// unity adsコールバック受け取り用メソッド
public void Ads(ShowResult res)
{
if (res == ShowResult.Failed)
{
// 再生失敗
}
else if (res == ShowResult.Skipped)
{
// スキップした場合
}
else if (res == ShowResult.Finished)
{
// 再生完了
if (gameSceneControlls)
{
// 指定のコールバックメソッド呼び出し。
gameSceneControlls.unityadsVideoFinishedCallBack();
}
}
}
// unity ads のcallback用イベントリスナー (僕の場合はad colonySDKと兼用
public GameManager gameSceneControlls;
public void setVideoSdkCallbackListener(GameManager controller)
{
gameSceneControlls = controller;
}
}
// 〜〜〜省略
// Unity Ads 初期化
GameSceneSingleton.Instance.initUnityAds();
// 〜〜〜省略
//再生できるか確認し、再生!
if(GameSceneSingleton.Instance.UnityAdsCanPlay()){
//Sound.StopBgm(); //BGM止めるなど
GameSceneSingleton.Instance.UnityAdsPlay();
}
// 〜〜〜省略
// unityads用callbackで呼ばれるメソッド
public void unityadsVideoFinishedCallBack(){
// Sound.PlayBgm ("bgm_status"); //BGM再開等
}
// 〜〜〜省略
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment