Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class AdWrapper : MonoBehaviour {
- public static bool isInitialized = false,
- isNeutralAdAvailable = false,
- isHelperAdAvailable = false,
- isSuccessAdAvailable = false,
- isGiftAdLoadProcess = false,
- isGiftAdAvailable = false,
- isEnergyAdLoadProcess = false,
- isEnergyAdAvailable = false;
- public static string filterNeutral = "", filterSuccess = "", filterHelper = "";
- protected static string neutralAdCaption, neutralAdBody, helperCaption, helperText, helperReward, helperBtnlabel, successText, successReward;
- void OnEnable(){
- //#if UNITY_ANDROID && !UNITY_EDITOR
- AdsorbEvents.adsorbOnNeutralRewardedAdReady += this.adsorbOnNeutralRewardedAdReady;
- AdsorbEvents.adsorbOnNeutralRewardedAdUnavailable += this.adsorbOnNeutralRewardedAdUnavailable;
- AdsorbEvents.adsorbOnHelperRewardedAdReady += this.adsorbOnHelperAdReady;
- AdsorbEvents.adsorbOnHelperRewardedAdUnavailable += this.adsorbOnHelperAdUnavailable;
- AdsorbEvents.adsorbOnSuccessRewardedAdReady += this.adsorbOnSuccessAdReady;
- AdsorbEvents.adsorbOnSuccessRewardedAdUnavailable += this.adsorbOnSuccessAdUnavailable;
- //#endif
- isInitialized = true;
- }
- void OnDisable(){
- //#if UNITY_ANDROID && !UNITY_EDITOR
- AdsorbEvents.adsorbOnNeutralRewardedAdReady -= this.adsorbOnNeutralRewardedAdReady;
- AdsorbEvents.adsorbOnNeutralRewardedAdUnavailable -= this.adsorbOnNeutralRewardedAdUnavailable;
- AdsorbEvents.adsorbOnHelperRewardedAdReady -= this.adsorbOnHelperAdReady;
- AdsorbEvents.adsorbOnHelperRewardedAdUnavailable -= this.adsorbOnHelperAdUnavailable;
- AdsorbEvents.adsorbOnSuccessRewardedAdReady -= this.adsorbOnSuccessAdReady;
- AdsorbEvents.adsorbOnSuccessRewardedAdUnavailable -= this.adsorbOnSuccessAdUnavailable;
- //#endif
- isInitialized = false;
- }
- public static void LoadEnergyAd(){
- if (!isInitialized) {
- Debug.Log ("WARNING: AdWrapper needs to be initialized before ads load");
- return;
- }
- if (!isEnergyAdAvailable && !isEnergyAdLoadProcess) {
- isEnergyAdAvailable = false;
- isEnergyAdLoadProcess = true;
- LoadNeutralAd("Free Energy", "Get 30 additional energy from our sponsor");
- }
- }
- public static void ShowEnergyAd(){
- if (!isInitialized) {
- Debug.Log ("WARNING: AdWrapper needs to be initialized before ads shown");
- return;
- }
- //#if UNITY_ANDROID && !UNITY_EDITOR
- if (isEnergyAdAvailable && isNeutralAdAvailable)
- AdsorbAds.ShowNeutralAd ("Free Energy", filterNeutral);
- //#endif
- }
- public static void LoadGiftAd(string reward){
- if (!isInitialized) {
- Debug.Log ("WARNING: AdWrapper needs to be initialized before ads load");
- return;
- }
- if (!isGiftAdAvailable && !isGiftAdLoadProcess) {
- successText = "Get more gift from our sponsor";
- successReward = reward;
- neutralAdCaption = "Extra Gift";
- neutralAdBody = successText;
- isGiftAdAvailable = false;
- isGiftAdLoadProcess = true;
- LoadHelperAd("Extra Gift", successText, successReward, "Claim");
- }
- }
- public static void ShowGiftAd(){
- Debug.Log ("Checking Gift Ad...");
- if (!isInitialized) {
- Debug.Log ("WARNING: AdWrapper needs to be initialized before ads shown");
- return;
- }
- //#if UNITY_ANDROID && !UNITY_EDITOR
- if (isGiftAdAvailable) {
- Debug.Log ("GIFT AD Available");
- if (isSuccessAdAvailable) {
- Debug.Log ("Showing Gift Ad : Success");
- AdsorbAds.ShowSuccessAd ("Extra Gift", filterSuccess);
- } else if (isHelperAdAvailable) {
- Debug.Log ("Showing Gift Ad : Helper");
- AdsorbAds.ShowHelperAd ("Extra Gift", filterHelper);
- } else if (isNeutralAdAvailable) {
- Debug.Log ("Showing Gift Ad : Neutral");
- AdsorbAds.ShowNeutralAd ("Extra Gift", filterNeutral);
- }
- }
- //#endif
- }
- public static void LoadNeutralAd(string caption, string body){
- #if UNITY_ANDROID && !UNITY_EDITOR
- isNeutralAdAvailable = false;
- neutralAdCaption = caption;
- neutralAdBody = body;
- AdsorbAds.LoadNeutralAd(neutralAdCaption, neutralAdBody, false);
- #endif
- }
- public static void LoadHelperAd(string caption, string text, string reward, string btnlabel){
- #if UNITY_ANDROID && !UNITY_EDITOR
- isHelperAdAvailable = false;
- helperCaption = caption;
- helperText = text;
- helperReward = reward;
- helperBtnlabel = btnlabel;
- AdsorbAds.LoadHelperAd(helperCaption, helperText, helperReward, helperBtnlabel, false);
- #endif
- }
- public static void LoadSuccessAd(string text, string reward){
- #if UNITY_ANDROID && !UNITY_EDITOR
- isSuccessAdAvailable = false;
- successText = text;
- successReward = reward;
- AdsorbAds.LoadSuccessAd (successText, successReward, false);
- #endif
- }
- #region LISTENERS
- public void adsorbOnNeutralRewardedAdReady(string filterName){
- Debug.Log("adsorbOnNeutralRewardedAdReady for filter '" + filterName + "'"
- + ", network: " + AdsorbAds.GetNeutralAdNetworkName(filterName)
- + ", type: " + AdsorbAds.GetNeutralAdNetworkType(filterName)
- + ", tags: " + AdsorbAds.GetNeutralAdNetworkTags(filterName)
- + ", has GUI: " + AdsorbAds.DoesNeutralAdProvideGUI(filterName));
- AdWrapper.filterNeutral = filterName;
- isNeutralAdAvailable = true;
- if (isEnergyAdLoadProcess) {
- isEnergyAdLoadProcess = false;
- isEnergyAdAvailable = true;
- } else if (isGiftAdLoadProcess) {
- isGiftAdLoadProcess = false;
- isGiftAdAvailable = false;
- }
- }
- public void adsorbOnNeutralRewardedAdUnavailable(string filterName){
- AdWrapper.filterNeutral = filterName;
- StartCoroutine(LoadNeutralAdCoroutine());
- }
- public void adsorbOnHelperAdReady(string filterName){
- Debug.Log("adsorbOnHelperAdReady for filter '" + filterName + "'"
- + ", network: " + AdsorbAds.GetHelperAdNetworkName(filterName)
- + ", type: " + AdsorbAds.GetHelperAdNetworkType(filterName)
- + ", tags: " + AdsorbAds.GetHelperAdNetworkTags(filterName)
- + ", has GUI: " + AdsorbAds.DoesHelperAdProvideGUI(filterName));
- AdWrapper.filterHelper = filterName;
- isNeutralAdAvailable = true;
- if (isGiftAdLoadProcess) {
- isGiftAdAvailable = true;
- isGiftAdLoadProcess = false;
- }
- }
- public void adsorbOnHelperAdUnavailable(string filterName){
- // Neutral ad unavailable
- AdWrapper.filterHelper = filterName;
- if (isGiftAdLoadProcess) {
- StartCoroutine (LoadSuccessAdCoroutine ());
- } else {
- StartCoroutine (LoadHelperAdCoroutine ());
- }
- }
- public void adsorbOnSuccessAdReady(string filterName){
- AdWrapper.filterSuccess = filterName;
- isSuccessAdAvailable = true;
- if (isGiftAdLoadProcess) {
- isGiftAdAvailable = true;
- isGiftAdLoadProcess = false;
- }
- }
- public void adsorbOnSuccessAdUnavailable(string filterName){
- Debug.Log("adsorbOnSuccessAdUnavailable for filter '" + filterName + "'"
- + ", network: " + AdsorbAds.GetSuccessAdNetworkName(filterName)
- + ", type: " + AdsorbAds.GetSuccessAdNetworkType(filterName)
- + ", tags: " + AdsorbAds.GetSuccessAdNetworkTags(filterName)
- + ", has GUI: " + AdsorbAds.DoesSuccessAdProvideGUI(filterName));
- AdWrapper.filterSuccess = filterName;
- if (isGiftAdLoadProcess) {
- StartCoroutine (LoadNeutralAdCoroutine ());
- } else {
- StartCoroutine (LoadSuccessAdCoroutine ());
- }
- }
- private IEnumerator LoadNeutralAdCoroutine(){
- yield return new WaitForSeconds(30.0f);
- LoadNeutralAd(neutralAdCaption, neutralAdBody);
- }
- private IEnumerator LoadHelperAdCoroutine(){
- yield return new WaitForSeconds(30.0f);
- if (!isHelperAdAvailable) {
- LoadHelperAd(helperCaption, helperText, helperReward, helperBtnlabel);
- }
- }
- private IEnumerator LoadSuccessAdCoroutine(){
- yield return new WaitForSeconds(30.0f);
- if (!isSuccessAdAvailable) {
- LoadSuccessAd (successText, successReward);
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement