Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using DLC.Purchases;
  5. using System;
  6. using Soomla.Store;
  7. using UnityEngine.SceneManagement;
  8. public class AdsManager : MonoBehaviour
  9. {
  10. private static bool isNoAdsPurchasedimmediatly = false;
  11. public static bool isNoAdsPurchased
  12. {
  13. get
  14. {
  15. try
  16. {
  17. return Convert.ToBoolean(StoreInventory.GetItemBalance(GoodOldPuzzlesStore.NO_ADS.ItemId)) || isPackageWithinADS;
  18. }
  19. catch (Exception e)
  20. {
  21. Debug.LogException(e);
  22. return false;
  23. }
  24. }
  25. }
  26.  
  27. private static bool isPackageWithinADS
  28. {
  29. get
  30. {
  31. return DLC.Content.GetActivePictureSet() != null && DLC.Content.GetActivePictureSet().Alias == Constants.EMBEDED_PACKAGE && SceneManager.GetActiveScene().name.Equals("game");
  32. }
  33.  
  34. }
  35. public static Action<bool> OnNoAdsPurchasedAction = null;
  36. private bool isUpdaterStarted = false;
  37. public static AdsManager Instance;
  38. private int BannerPosition = 0;
  39. private const int BANNER_REFRESH_TIME = 30;
  40. private List<IAdBannerController> AdsStorageList = new List<IAdBannerController>();
  41. private ADCAdManager adColonyManager;
  42. private void Awake()
  43. {
  44. if (Instance != null)
  45. DestroyImmediate(gameObject);
  46. else
  47. {
  48. DontDestroyOnLoad(gameObject);
  49. Instance = this;
  50. }
  51. }
  52. private void Start()
  53. {
  54. if (isNoAdsPurchased)
  55. {
  56. GameObject.Destroy(this.gameObject);
  57. }
  58. adColonyManager = new ADCAdManager ();
  59. #if PLATFORM_AMAZON
  60. AdsStorageList.Add(new ManagerAdMob(Constants.AmazonAdmobBannerAdUnitId));
  61. #elif UNITY_IOS
  62. AdsStorageList.Add(new ManagerAdMob(Constants.IosAdmobBannerAdUnitId));
  63. #else
  64. AdsStorageList.Add(new ManagerInmobi(Constants.INMOBI_BANNER_ID,Constants.INMOBI_PROPERTY_ID,this));
  65. #endif
  66. }
  67.  
  68. private void _HideBanners()
  69. {
  70. #if !UNITY_EDITOR
  71. foreach (IAdBannerController adController in AdsStorageList)
  72. {
  73. adController.HideBanner();
  74. }
  75. #endif
  76. }
  77.  
  78. private void _UpdateBannerVisibility()
  79. {
  80. #if !UNITY_EDITOR
  81. _HideBanners();
  82. if (AdsStorageList[BannerPosition] != null)
  83. {
  84. Debug.Log ("CURRENT BANNER TO SHOW = "+AdsStorageList[BannerPosition].AdsType().ToString());
  85. AdsStorageList[BannerPosition].ShowBanner();
  86. }
  87. BannerPosition = BannerPosition + 1 >= AdsStorageList.Count ? 0 : BannerPosition + 1;
  88. #endif
  89. }
  90.  
  91. private void _DestroyBannerUpdater()
  92. {
  93. _HideBanners ();
  94. foreach (IAdBannerController adController in AdsStorageList)
  95. {
  96. adController.DestroyAd();
  97. }
  98. StopAllCoroutines();
  99. Instance = null;
  100. GameObject.Destroy(this.gameObject);
  101. }
  102.  
  103. private void OnNoAdsPurchased()
  104. {
  105. if (OnNoAdsPurchasedAction != null) {
  106. OnNoAdsPurchasedAction (false);
  107. }
  108. _DestroyBannerUpdater();
  109. }
  110.  
  111. private IEnumerator UpdateBannersVisibilityCorutine()
  112. {
  113. while (true)
  114. {
  115. _UpdateBannerVisibility();
  116. yield return new WaitForSeconds(BANNER_REFRESH_TIME);
  117. }
  118. }
  119.  
  120. private void _HideBanner()
  121. {
  122. StopAllCoroutines();
  123. Instance._HideBanners();
  124. }
  125. private void _ShowBanner()
  126. {
  127. StopAllCoroutines();
  128. if (AdsStorageList.Count > 1) {
  129. StartCoroutine (UpdateBannersVisibilityCorutine ());
  130. } else {
  131. _UpdateBannerVisibility ();
  132. }
  133. }
  134.  
  135. // вызывать для показа баннеров
  136. public static void ShowBanner()
  137. {
  138. if (Instance != null && !isNoAdsPurchased)
  139. {
  140. Instance._ShowBanner();
  141. }
  142. }
  143.  
  144. public static void RequestInterstital()
  145. {
  146. if (!isNoAdsPurchased && Instance != null)
  147. {
  148. Instance.adColonyManager.RequestInterstitial();
  149. }
  150. }
  151.  
  152. public static bool IsFullscreenLoaded (){
  153. return Instance !=null ? Instance.adColonyManager.IsLoaded() : false;
  154. }
  155.  
  156. public static void ShowInterstitial (){
  157. if (Instance!=null && !isNoAdsPurchased){
  158. Instance.adColonyManager.PlayAd();
  159. }
  160. }
  161.  
  162. // вызывать для скрытия баннеров
  163. public static void HideBanner()
  164. {
  165. if (Instance != null)
  166. {
  167. Instance._HideBanner();
  168. }
  169. }
  170. // вызывать только для покупки инаппа отключения рекламы
  171. public static void GiveNoAdsItemToUser()
  172. {
  173. isNoAdsPurchasedimmediatly = true;
  174. if (!isNoAdsPurchased)
  175. {
  176.  
  177. Debug.Log("GiveNoAdsItemToUser ");
  178. StoreInventory.GiveItem(GoodOldPuzzlesStore.NO_ADS.ItemId,1);
  179. RectTransformChanger.СhangePosition();
  180. //SaveGameManager.RemoveAllSave();
  181. }
  182. if (Instance!=null){
  183. Instance.OnNoAdsPurchased();
  184. }
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement