Advertisement
aminusia

adsWrapper

May 11th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.67 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AdWrapper : MonoBehaviour {
  5.     public static bool isInitialized = false,
  6.     isNeutralAdAvailable = false,
  7.     isHelperAdAvailable = false,
  8.     isSuccessAdAvailable = false,
  9.     isGiftAdLoadProcess = false,
  10.     isGiftAdAvailable = false,
  11.     isEnergyAdLoadProcess = false,
  12.     isEnergyAdAvailable = false;
  13.     public static string filterNeutral = "", filterSuccess = "", filterHelper = "";
  14.     protected static string neutralAdCaption, neutralAdBody, helperCaption, helperText, helperReward, helperBtnlabel, successText, successReward;
  15.  
  16.     void OnEnable(){
  17.         //#if UNITY_ANDROID && !UNITY_EDITOR
  18.         AdsorbEvents.adsorbOnNeutralRewardedAdReady += this.adsorbOnNeutralRewardedAdReady;
  19.         AdsorbEvents.adsorbOnNeutralRewardedAdUnavailable += this.adsorbOnNeutralRewardedAdUnavailable;
  20.  
  21.         AdsorbEvents.adsorbOnHelperRewardedAdReady += this.adsorbOnHelperAdReady;
  22.         AdsorbEvents.adsorbOnHelperRewardedAdUnavailable += this.adsorbOnHelperAdUnavailable;
  23.  
  24.         AdsorbEvents.adsorbOnSuccessRewardedAdReady += this.adsorbOnSuccessAdReady;
  25.         AdsorbEvents.adsorbOnSuccessRewardedAdUnavailable += this.adsorbOnSuccessAdUnavailable;
  26.         //#endif
  27.         isInitialized = true;
  28.     }
  29.    
  30.     void OnDisable(){
  31.         //#if UNITY_ANDROID && !UNITY_EDITOR
  32.         AdsorbEvents.adsorbOnNeutralRewardedAdReady -= this.adsorbOnNeutralRewardedAdReady;
  33.         AdsorbEvents.adsorbOnNeutralRewardedAdUnavailable -= this.adsorbOnNeutralRewardedAdUnavailable;
  34.  
  35.         AdsorbEvents.adsorbOnHelperRewardedAdReady -= this.adsorbOnHelperAdReady;
  36.         AdsorbEvents.adsorbOnHelperRewardedAdUnavailable -= this.adsorbOnHelperAdUnavailable;
  37.  
  38.         AdsorbEvents.adsorbOnSuccessRewardedAdReady -= this.adsorbOnSuccessAdReady;
  39.         AdsorbEvents.adsorbOnSuccessRewardedAdUnavailable -= this.adsorbOnSuccessAdUnavailable;
  40.         //#endif
  41.         isInitialized = false;
  42.     }
  43.  
  44.     public static void LoadEnergyAd(){
  45.         if (!isInitialized) {
  46.             Debug.Log ("WARNING: AdWrapper needs to be initialized before ads load");
  47.             return;
  48.         }
  49.         if (!isEnergyAdAvailable && !isEnergyAdLoadProcess) {
  50.             isEnergyAdAvailable = false;
  51.             isEnergyAdLoadProcess = true;
  52.             LoadNeutralAd("Free Energy", "Get 30 additional energy from our sponsor");
  53.         }
  54.     }
  55.  
  56.     public static void ShowEnergyAd(){
  57.         if (!isInitialized) {
  58.             Debug.Log ("WARNING: AdWrapper needs to be initialized before ads shown");
  59.             return;
  60.         }
  61.         //#if UNITY_ANDROID && !UNITY_EDITOR
  62.         if (isEnergyAdAvailable && isNeutralAdAvailable)
  63.             AdsorbAds.ShowNeutralAd ("Free Energy", filterNeutral);
  64.         //#endif
  65.     }
  66.  
  67.     public static void LoadGiftAd(string reward){
  68.         if (!isInitialized) {
  69.             Debug.Log ("WARNING: AdWrapper needs to be initialized before ads load");
  70.             return;
  71.         }
  72.         if (!isGiftAdAvailable && !isGiftAdLoadProcess) {
  73.             successText = "Get more gift from our sponsor";
  74.             successReward = reward;
  75.             neutralAdCaption = "Extra Gift";
  76.             neutralAdBody = successText;
  77.             isGiftAdAvailable = false;
  78.             isGiftAdLoadProcess = true;
  79.             LoadHelperAd("Extra Gift", successText, successReward, "Claim");
  80.         }
  81.     }
  82.  
  83.     public static void ShowGiftAd(){
  84.         Debug.Log ("Checking Gift Ad...");
  85.         if (!isInitialized) {
  86.             Debug.Log ("WARNING: AdWrapper needs to be initialized before ads shown");
  87.             return;
  88.         }
  89.         //#if UNITY_ANDROID && !UNITY_EDITOR
  90.         if (isGiftAdAvailable) {
  91.             Debug.Log ("GIFT AD Available");
  92.             if (isSuccessAdAvailable) {
  93.                 Debug.Log ("Showing Gift Ad : Success");
  94.                 AdsorbAds.ShowSuccessAd ("Extra Gift", filterSuccess);
  95.             } else if (isHelperAdAvailable) {
  96.                 Debug.Log ("Showing Gift Ad : Helper");
  97.                 AdsorbAds.ShowHelperAd ("Extra Gift", filterHelper);
  98.             } else if (isNeutralAdAvailable) {
  99.                 Debug.Log ("Showing Gift Ad : Neutral");
  100.                 AdsorbAds.ShowNeutralAd ("Extra Gift", filterNeutral);
  101.             }
  102.         }
  103.         //#endif
  104.     }
  105.  
  106.     public static void LoadNeutralAd(string caption, string body){
  107.         #if UNITY_ANDROID && !UNITY_EDITOR
  108.         isNeutralAdAvailable = false;
  109.         neutralAdCaption = caption;
  110.         neutralAdBody = body;
  111.         AdsorbAds.LoadNeutralAd(neutralAdCaption, neutralAdBody, false);
  112.         #endif
  113.     }
  114.  
  115.     public static void LoadHelperAd(string caption, string text, string reward, string btnlabel){
  116.         #if UNITY_ANDROID && !UNITY_EDITOR
  117.         isHelperAdAvailable = false;
  118.         helperCaption = caption;
  119.         helperText = text;
  120.         helperReward = reward;
  121.         helperBtnlabel = btnlabel;
  122.         AdsorbAds.LoadHelperAd(helperCaption, helperText, helperReward, helperBtnlabel, false);
  123.         #endif
  124.     }
  125.  
  126.     public static void LoadSuccessAd(string text, string reward){
  127.         #if UNITY_ANDROID && !UNITY_EDITOR
  128.         isSuccessAdAvailable = false;
  129.         successText = text;
  130.         successReward = reward;
  131.         AdsorbAds.LoadSuccessAd (successText, successReward, false);
  132.         #endif
  133.     }
  134.  
  135.     #region LISTENERS
  136.     public void adsorbOnNeutralRewardedAdReady(string filterName){
  137.        
  138.         Debug.Log("adsorbOnNeutralRewardedAdReady for filter '" + filterName + "'"
  139.             + ", network: " + AdsorbAds.GetNeutralAdNetworkName(filterName)
  140.             + ", type: " + AdsorbAds.GetNeutralAdNetworkType(filterName)
  141.             + ", tags: " + AdsorbAds.GetNeutralAdNetworkTags(filterName)
  142.             + ", has GUI: " + AdsorbAds.DoesNeutralAdProvideGUI(filterName));
  143.        
  144.         AdWrapper.filterNeutral = filterName;
  145.         isNeutralAdAvailable = true;
  146.         if (isEnergyAdLoadProcess) {
  147.             isEnergyAdLoadProcess = false;
  148.             isEnergyAdAvailable = true;
  149.         } else if (isGiftAdLoadProcess) {
  150.             isGiftAdLoadProcess = false;
  151.             isGiftAdAvailable = false;
  152.         }
  153.     }
  154.  
  155.     public void adsorbOnNeutralRewardedAdUnavailable(string filterName){
  156.         AdWrapper.filterNeutral = filterName;
  157.         StartCoroutine(LoadNeutralAdCoroutine());
  158.     }
  159.  
  160.     public void adsorbOnHelperAdReady(string filterName){
  161.        
  162.         Debug.Log("adsorbOnHelperAdReady for filter '" + filterName + "'"
  163.             + ", network: " + AdsorbAds.GetHelperAdNetworkName(filterName)
  164.             + ", type: " + AdsorbAds.GetHelperAdNetworkType(filterName)
  165.             + ", tags: " + AdsorbAds.GetHelperAdNetworkTags(filterName)
  166.             + ", has GUI: " + AdsorbAds.DoesHelperAdProvideGUI(filterName));
  167.        
  168.         AdWrapper.filterHelper = filterName;
  169.         isNeutralAdAvailable = true;
  170.         if (isGiftAdLoadProcess) {
  171.             isGiftAdAvailable = true;
  172.             isGiftAdLoadProcess = false;
  173.         }
  174.     }
  175.  
  176.     public void adsorbOnHelperAdUnavailable(string filterName){
  177.         // Neutral ad unavailable
  178.         AdWrapper.filterHelper = filterName;
  179.         if (isGiftAdLoadProcess) {
  180.             StartCoroutine (LoadSuccessAdCoroutine ());
  181.         } else {
  182.             StartCoroutine (LoadHelperAdCoroutine ());
  183.         }
  184.     }
  185.  
  186.     public void adsorbOnSuccessAdReady(string filterName){
  187.         AdWrapper.filterSuccess = filterName;
  188.         isSuccessAdAvailable = true;
  189.         if (isGiftAdLoadProcess) {
  190.             isGiftAdAvailable = true;
  191.             isGiftAdLoadProcess = false;
  192.         }
  193.     }
  194.  
  195.     public void adsorbOnSuccessAdUnavailable(string filterName){
  196.        
  197.         Debug.Log("adsorbOnSuccessAdUnavailable for filter '" + filterName + "'"
  198.             + ", network: " + AdsorbAds.GetSuccessAdNetworkName(filterName)
  199.             + ", type: " + AdsorbAds.GetSuccessAdNetworkType(filterName)
  200.             + ", tags: " + AdsorbAds.GetSuccessAdNetworkTags(filterName)
  201.             + ", has GUI: " + AdsorbAds.DoesSuccessAdProvideGUI(filterName));
  202.        
  203.         AdWrapper.filterSuccess = filterName;
  204.         if (isGiftAdLoadProcess) {
  205.             StartCoroutine (LoadNeutralAdCoroutine ());
  206.         } else {
  207.             StartCoroutine (LoadSuccessAdCoroutine ());
  208.         }
  209.     }
  210.  
  211.     private IEnumerator LoadNeutralAdCoroutine(){
  212.         yield return new WaitForSeconds(30.0f);
  213.         LoadNeutralAd(neutralAdCaption, neutralAdBody);
  214.     }
  215.  
  216.     private IEnumerator LoadHelperAdCoroutine(){
  217.         yield return new WaitForSeconds(30.0f);
  218.         if (!isHelperAdAvailable) {
  219.             LoadHelperAd(helperCaption, helperText, helperReward, helperBtnlabel);
  220.         }
  221.     }
  222.  
  223.     private IEnumerator LoadSuccessAdCoroutine(){
  224.         yield return new WaitForSeconds(30.0f);
  225.         if (!isSuccessAdAvailable) {
  226.             LoadSuccessAd (successText, successReward);
  227.         }
  228.     }
  229.     #endregion
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement