Last active
April 4, 2017 03:49
-
-
Save matsuyoro/793c8fe747feb3199f909e34dcd920e5 to your computer and use it in GitHub Desktop.
iOS/Android(Unity5.5)で Admobメディエーションで、バナー広告、インタースティシャル広告、動画リワード広告を、最適化した色んな会社の広告をシングルトンで一元化して出す方法 ref: http://qiita.com/matsuyoro/items/33d723fdef798f74d286
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 〜〜省略 | |
GameSceneSingleton.Instance.setVideoSdkCallbackListener(this); | |
// 〜〜省略 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GameSceneSingleton { | |
private static GameSceneSingleton mInstance; | |
private GameSceneSingleton () { // Private Constructor | |
} | |
public static GameSceneSingleton Instance { | |
get { | |
if( mInstance == null ) mInstance = new GameSceneSingleton(); | |
return mInstance; | |
} | |
} | |
/********** | |
* admob インタースティシャルメディエーション用シングルトン | |
**********/ | |
private InterstitialAd interstitial; | |
private AdRequest request; | |
private BannerView bannerView; | |
bool is_close_interstitial = false; | |
// バナーリクエスト | |
public void RequestBanner() | |
{ | |
#if UNITY_ANDROID | |
string adUnitId = Constant.SDK_ADMOB_BANNER_UNIT_ID; | |
#elif UNITY_IPHONE | |
string adUnitId = Constant.SDK_ADMOB_BANNER_UNIT_ID; | |
#else | |
string adUnitId = Constant.SDK_ADMOB_BANNER_UNIT_ID; | |
#endif | |
// AdSize adSize = new AdSize(50, 320); //縦長カスタマイズしたら | |
// Create a 320x50 banner at the top of the screen. | |
// bannerView = new BannerView(adUnitId, adSize, AdPosition.Bottom); | |
bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.BottomRight); | |
// テストデバイス指定 | |
AdRequest request = new AdRequest.Builder() | |
.AddTestDevice(AdRequest.TestDeviceSimulator) // Simulator. | |
.AddTestDevice("*************") // My test device | |
.Build(); | |
// Load the banner with the request. | |
bannerView.LoadAd(request); | |
bannerView.Hide (); //最初は一旦非表示 | |
} | |
// バナー非表示 | |
public void BannerHide(){ | |
if (bannerView != null) { | |
bannerView.Hide (); | |
} | |
} | |
// バナー非表示 | |
public void BannerShow(){ | |
if (bannerView != null) | |
{ | |
bannerView.Show (); | |
} | |
} | |
// インスティリクエスト | |
public void RequestInterstitial() | |
{ | |
#if UNITY_ANDROID | |
string adUnitId = Constant.SDK_ADMOB_INSTI_UNIT_ID; | |
#elif UNITY_IPHONE | |
string adUnitId = Constant.SDK_ADMOB_INSTI_UNIT_ID; | |
#else | |
string adUnitId = Constant.SDK_ADMOB_INSTI_UNIT_ID; | |
#endif | |
if (is_close_interstitial == true) { | |
interstitial.Destroy (); | |
} | |
// Initialize an InterstitialAd. | |
interstitial = new InterstitialAd (adUnitId); | |
// Create an empty ad request. | |
// request = new AdRequest.Builder ().Build (); | |
// テストデバイス指定 | |
AdRequest request = new AdRequest.Builder() | |
.AddTestDevice(AdRequest.TestDeviceSimulator) // Simulator. | |
.AddTestDevice("***************") // My test device | |
.Build(); | |
// Load the interstitial with the request. | |
interstitial.LoadAd (request); | |
interstitial.OnAdClosed += HandleAdClosed; | |
// interstitial.AdClosed += HandleAdClosed; | |
is_close_interstitial = false; | |
} | |
// インタースティシャル広告を閉じた時に走る | |
void HandleAdClosed (object sender, System.EventArgs e) | |
{ | |
is_close_interstitial = true; | |
} | |
// インタースティシャル非表示 | |
public void InterstitialDestroy(){ | |
if (is_close_interstitial == true) { | |
interstitial.Destroy (); | |
} | |
RequestInterstitial (); | |
} | |
// インタースティシャル表示用 | |
public void InterstitialShow(){ | |
if (interstitial.IsLoaded ()) { | |
interstitial.Show (); | |
} | |
} | |
// admob 動画リワードメディエーション用シングルトン | |
private RewardBasedVideoAd rewardBasedVideo; | |
public void RequestRewardBasedVideo() | |
{ | |
// 各SDKの初期化 | |
// AppLovin.SetVerboseLoggingOn("true"); | |
#if UNITY_ANDROID | |
// TODO 一旦激重になる不具合のため未対応 | |
// AppLovin | |
AppLovin.InitializeSdk(); | |
// Preload a rewarded video after you initialize. | |
AppLovin.LoadRewardedInterstitial(); | |
// AdColony | |
// AdColony.Configure ("version:1.0,store:google", Constant.SDK_ADC_APP_ID, Constant.SDK_ADC_ZONE_ID); | |
#elif UNITY_IOS | |
// Nendインタースティシャル初期化 | |
// StartCoroutine (NendLoad()); //Nendロード 非同期で呼ばないとアプリに影響あり | |
// AppLovin動画リワードSDK | |
AppLovin.SetSdkKey(Constant.SDK_APPLOVIN_API_KEY); | |
AppLovin.InitializeSdk(); | |
// AdColonySDK | |
AdColony.Configure("1.0", Constant.SDK_ADC_APP_ID, Constant.SDK_ADC_ZONE_ID); | |
#endif | |
string adUnitId; | |
#if UNITY_EDITOR | |
adUnitId = "unused"; | |
#elif UNITY_ANDROID | |
adUnitId = Constant.SDK_ADMOB_VIDEO_REWARD_AD_UNIT_ID; | |
#elif UNITY_IOS | |
adUnitId = Constant.SDK_ADMOB_VIDEO_REWARD_AD_UNIT_ID; | |
#else | |
adUnitId = "unexpected_platform"; | |
#endif | |
rewardBasedVideo = RewardBasedVideoAd.Instance; | |
// テストデバイス指定 | |
AdRequest request = new AdRequest.Builder() | |
.AddTestDevice(AdRequest.TestDeviceSimulator) // Simulator. | |
.AddTestDevice("**************") // My test device | |
.Build(); | |
rewardBasedVideo.LoadAd(request, adUnitId); | |
// has rewarded the user. | |
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded; | |
rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoRewardedClosed; | |
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoRewardedFailed; | |
} | |
public GameManager gameSceneControlls; | |
public void setVideoSdkCallbackListener(GameManager controller) | |
{ | |
gameSceneControlls = controller; | |
} | |
// 動画再生 | |
public void pushAds() | |
{ | |
#if UNITY_EDITOR | |
#elif UNITY_ANDROID | |
if (rewardBasedVideo.IsLoaded()) //admob動画リワードメディエーションが読み込み完了なら | |
{ | |
rewardBasedVideo.Show(); | |
} | |
#elif UNITY_IOS | |
if (rewardBasedVideo.IsLoaded()) //admob動画リワードメディエーションが読み込み完了なら | |
{ | |
rewardBasedVideo.Show(); | |
} | |
#else | |
#endif | |
} | |
// 読み込み可能かチェック | |
public bool madiationIsLoaded() | |
{ | |
#if UNITY_EDITOR | |
return true; | |
#elif UNITY_ANDROID | |
return rewardBasedVideo.IsLoaded(); | |
#elif UNITY_IOS | |
return rewardBasedVideo.IsLoaded(); | |
#else | |
return false; | |
#endif | |
} | |
// 報酬受け渡し処理 | |
void HandleRewardBasedVideoRewarded(object sender, Reward args) | |
{ | |
} | |
// 動画広告リロード | |
void HandleRewardBasedVideoRewardedClosed(object sender, System.EventArgs args) | |
{ | |
RequestRewardBasedVideo(); | |
if (gameSceneControlls) | |
{ | |
// 指定の視聴完了コールバックメソッド呼び出し。 | |
gameSceneControlls.mediationVideoFinishedCallBack(); | |
} | |
} | |
// ロード失敗時 | |
void HandleRewardBasedVideoRewardedFailed(object sender, AdFailedToLoadEventArgs args) | |
{ | |
// ここ要確認、無線状態になると永遠にadmobにリクエスト送りまくるので扱い注意 | |
RequestRewardBasedVideo(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment