Skip to content

Instantly share code, notes, and snippets.

@sukedon
Last active December 28, 2015 23:30
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 sukedon/5de91a6b33ce17258b69 to your computer and use it in GitHub Desktop.
Save sukedon/5de91a6b33ce17258b69 to your computer and use it in GitHub Desktop.
unity5.2以前で使えるUnityAdsのhelperクラス
using UnityEngine;
using UnityEngine.Advertisements;
using System.Collections;
public class UnityAdsHelper : MonoBehaviour {
#region SerializeField
/// <summary>
/// The android game identifier.
/// </summary>
[SerializeField]
private string androidGameId;
/// <summary>
/// The ios game identifier.
/// </summary>
[SerializeField]
private string iosGameId;
/// <summary>
/// Debug mode flag.
/// </summary>
[SerializeField]
private bool isDebugMode;
/// <summary>
/// 広告表示
/// </summary>
[SerializeField]
private int showAdInterval;
#endregion
#region privateField
/// <summary>
/// 広告表示回数カウンター
/// </summary>
private int showAdCount;
#endregion
// Use this for initialization
void Awake() {
string gameId = "";
#if UNITY_IOS
gameid = iosGameId;
#elif UNITY_ANDROID
gameId = androidGameId;
#endif
Advertisement.UnityDeveloperInternalTestMode = isDebugMode;
if(string.IsNullOrEmpty(gameId)) {
Debug.Log("gameid is null or empty");
return;
}
if (Advertisement.isSupported) {
Advertisement.Initialize (gameId);
} else {
Debug.Log("Platform not supported");
}
showAdCount = showAdInterval - 1;
}
/// <summary>
/// Determines whether this instance can show ads.
/// 表示準備が出来ていたら、3回に1回表示する
/// </summary>
/// <returns><c>true</c> if this instance can show ads; otherwise, <c>false</c>.</returns>
public bool CanShowAds(){
if (!Advertisement.IsReady ())return false;
showAdCount++;
if(showAdCount >= showAdInterval){
showAdCount = 0;
return true;
}else{
return false;
}
}
public void ShowAdvertisementAndEarn(System.Action callback = null){
Advertisement.Show(null, new ShowOptions {
resultCallback = result => {
switch(result){
case ShowResult.Finished:
//例: コイン付与処理
PlayerState.Instance.Coin.Value += 20;
break;
}
if(callback != null) callback();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment