Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using DLC.Purchases;
- using System;
- using Soomla.Store;
- using UnityEngine.SceneManagement;
- public class AdsManager : MonoBehaviour
- {
- private static bool isNoAdsPurchasedimmediatly = false;
- public static bool isNoAdsPurchased
- {
- get
- {
- try
- {
- return Convert.ToBoolean(StoreInventory.GetItemBalance(GoodOldPuzzlesStore.NO_ADS.ItemId)) || isPackageWithinADS;
- }
- catch (Exception e)
- {
- Debug.LogException(e);
- return false;
- }
- }
- }
- private static bool isPackageWithinADS
- {
- get
- {
- return DLC.Content.GetActivePictureSet() != null && DLC.Content.GetActivePictureSet().Alias == Constants.EMBEDED_PACKAGE && SceneManager.GetActiveScene().name.Equals("game");
- }
- }
- public static Action<bool> OnNoAdsPurchasedAction = null;
- private bool isUpdaterStarted = false;
- public static AdsManager Instance;
- private int BannerPosition = 0;
- private const int BANNER_REFRESH_TIME = 30;
- private List<IAdBannerController> AdsStorageList = new List<IAdBannerController>();
- private ADCAdManager adColonyManager;
- private void Awake()
- {
- if (Instance != null)
- DestroyImmediate(gameObject);
- else
- {
- DontDestroyOnLoad(gameObject);
- Instance = this;
- }
- }
- private void Start()
- {
- if (isNoAdsPurchased)
- {
- GameObject.Destroy(this.gameObject);
- }
- adColonyManager = new ADCAdManager ();
- #if PLATFORM_AMAZON
- AdsStorageList.Add(new ManagerAdMob(Constants.AmazonAdmobBannerAdUnitId));
- #elif UNITY_IOS
- AdsStorageList.Add(new ManagerAdMob(Constants.IosAdmobBannerAdUnitId));
- #else
- AdsStorageList.Add(new ManagerInmobi(Constants.INMOBI_BANNER_ID,Constants.INMOBI_PROPERTY_ID,this));
- #endif
- }
- private void _HideBanners()
- {
- #if !UNITY_EDITOR
- foreach (IAdBannerController adController in AdsStorageList)
- {
- adController.HideBanner();
- }
- #endif
- }
- private void _UpdateBannerVisibility()
- {
- #if !UNITY_EDITOR
- _HideBanners();
- if (AdsStorageList[BannerPosition] != null)
- {
- Debug.Log ("CURRENT BANNER TO SHOW = "+AdsStorageList[BannerPosition].AdsType().ToString());
- AdsStorageList[BannerPosition].ShowBanner();
- }
- BannerPosition = BannerPosition + 1 >= AdsStorageList.Count ? 0 : BannerPosition + 1;
- #endif
- }
- private void _DestroyBannerUpdater()
- {
- _HideBanners ();
- foreach (IAdBannerController adController in AdsStorageList)
- {
- adController.DestroyAd();
- }
- StopAllCoroutines();
- Instance = null;
- GameObject.Destroy(this.gameObject);
- }
- private void OnNoAdsPurchased()
- {
- if (OnNoAdsPurchasedAction != null) {
- OnNoAdsPurchasedAction (false);
- }
- _DestroyBannerUpdater();
- }
- private IEnumerator UpdateBannersVisibilityCorutine()
- {
- while (true)
- {
- _UpdateBannerVisibility();
- yield return new WaitForSeconds(BANNER_REFRESH_TIME);
- }
- }
- private void _HideBanner()
- {
- StopAllCoroutines();
- Instance._HideBanners();
- }
- private void _ShowBanner()
- {
- StopAllCoroutines();
- if (AdsStorageList.Count > 1) {
- StartCoroutine (UpdateBannersVisibilityCorutine ());
- } else {
- _UpdateBannerVisibility ();
- }
- }
- // вызывать для показа баннеров
- public static void ShowBanner()
- {
- if (Instance != null && !isNoAdsPurchased)
- {
- Instance._ShowBanner();
- }
- }
- public static void RequestInterstital()
- {
- if (!isNoAdsPurchased && Instance != null)
- {
- Instance.adColonyManager.RequestInterstitial();
- }
- }
- public static bool IsFullscreenLoaded (){
- return Instance !=null ? Instance.adColonyManager.IsLoaded() : false;
- }
- public static void ShowInterstitial (){
- if (Instance!=null && !isNoAdsPurchased){
- Instance.adColonyManager.PlayAd();
- }
- }
- // вызывать для скрытия баннеров
- public static void HideBanner()
- {
- if (Instance != null)
- {
- Instance._HideBanner();
- }
- }
- // вызывать только для покупки инаппа отключения рекламы
- public static void GiveNoAdsItemToUser()
- {
- isNoAdsPurchasedimmediatly = true;
- if (!isNoAdsPurchased)
- {
- Debug.Log("GiveNoAdsItemToUser ");
- StoreInventory.GiveItem(GoodOldPuzzlesStore.NO_ADS.ItemId,1);
- RectTransformChanger.СhangePosition();
- //SaveGameManager.RemoveAllSave();
- }
- if (Instance!=null){
- Instance.OnNoAdsPurchased();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement