AdMobGmaBridge.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #if CRAZY_FRUIT_ADMOB
  2. using System;
  3. using GoogleMobileAds.Api;
  4. /// <summary>
  5. /// Google Mobile Ads bridge. Enable with scripting define CRAZY_FRUIT_ADMOB.
  6. /// </summary>
  7. public static class AdMobGmaBridge
  8. {
  9. static RewardedAd rewardedAd;
  10. static InterstitialAd interstitialAd;
  11. static AppOpenAd appOpenAd;
  12. static DateTime appOpenLoadUtc;
  13. static BannerView bannerView;
  14. public static event Action FullScreenAdOpened;
  15. public static event Action FullScreenAdClosed;
  16. public static void Initialize(string androidAppId, string iosAppId, Action onComplete)
  17. {
  18. #if UNITY_IOS
  19. MobileAds.SetiOSAppPauseOnBackground(true);
  20. #endif
  21. MobileAds.Initialize(_ =>
  22. {
  23. onComplete?.Invoke();
  24. });
  25. }
  26. public static void LoadRewarded(string unitId, Action onLoaded, Action<string> onFailed)
  27. {
  28. if (rewardedAd != null)
  29. {
  30. rewardedAd.Destroy();
  31. rewardedAd = null;
  32. }
  33. RewardedAd.Load(unitId, new AdRequest(), (ad, error) =>
  34. {
  35. if (error != null || ad == null)
  36. {
  37. onFailed?.Invoke(error != null ? error.GetMessage() : "LOAD FAILED");
  38. return;
  39. }
  40. rewardedAd = ad;
  41. RegisterFullScreenCallbacks(rewardedAd);
  42. onLoaded?.Invoke();
  43. });
  44. }
  45. public static void ShowRewarded(Action onReward, Action<string> onFailed)
  46. {
  47. if (rewardedAd == null || !rewardedAd.CanShowAd())
  48. {
  49. onFailed?.Invoke("CANNOT SHOW");
  50. return;
  51. }
  52. bool rewarded = false;
  53. rewardedAd.OnAdFullScreenContentClosed += () =>
  54. {
  55. if (!rewarded)
  56. onFailed?.Invoke("CLOSED EARLY");
  57. };
  58. rewardedAd.OnAdFullScreenContentFailed += error =>
  59. {
  60. onFailed?.Invoke(error != null ? error.GetMessage() : "SHOW FAILED");
  61. };
  62. rewardedAd.Show(reward =>
  63. {
  64. rewarded = true;
  65. onReward?.Invoke();
  66. });
  67. }
  68. public static void LoadInterstitial(string unitId, Action onLoaded, Action<string> onFailed)
  69. {
  70. if (interstitialAd != null)
  71. {
  72. interstitialAd.Destroy();
  73. interstitialAd = null;
  74. }
  75. InterstitialAd.Load(unitId, new AdRequest(), (ad, error) =>
  76. {
  77. if (error != null || ad == null)
  78. {
  79. onFailed?.Invoke(error != null ? error.GetMessage() : "LOAD FAILED");
  80. return;
  81. }
  82. interstitialAd = ad;
  83. RegisterFullScreenCallbacks(interstitialAd);
  84. onLoaded?.Invoke();
  85. });
  86. }
  87. public static bool CanShowInterstitial()
  88. {
  89. return interstitialAd != null && interstitialAd.CanShowAd();
  90. }
  91. public static void ShowInterstitial(Action onClosed, Action<string> onFailed)
  92. {
  93. if (!CanShowInterstitial())
  94. {
  95. onFailed?.Invoke("CANNOT SHOW");
  96. return;
  97. }
  98. bool completed = false;
  99. interstitialAd.OnAdFullScreenContentClosed += () =>
  100. {
  101. completed = true;
  102. onClosed?.Invoke();
  103. };
  104. interstitialAd.OnAdFullScreenContentFailed += error =>
  105. {
  106. if (!completed)
  107. onFailed?.Invoke(error != null ? error.GetMessage() : "SHOW FAILED");
  108. };
  109. interstitialAd.Show();
  110. }
  111. public static void DestroyInterstitial()
  112. {
  113. if (interstitialAd == null)
  114. return;
  115. interstitialAd.Destroy();
  116. interstitialAd = null;
  117. }
  118. public static void LoadAppOpen(string unitId, Action onLoaded, Action<string> onFailed)
  119. {
  120. if (appOpenAd != null)
  121. {
  122. appOpenAd.Destroy();
  123. appOpenAd = null;
  124. }
  125. AppOpenAd.Load(unitId, new AdRequest(), (ad, error) =>
  126. {
  127. if (error != null || ad == null)
  128. {
  129. onFailed?.Invoke(error != null ? error.GetMessage() : "LOAD FAILED");
  130. return;
  131. }
  132. appOpenAd = ad;
  133. appOpenLoadUtc = DateTime.UtcNow;
  134. RegisterFullScreenCallbacks(appOpenAd);
  135. onLoaded?.Invoke();
  136. });
  137. }
  138. public static bool CanShowAppOpen()
  139. {
  140. if (appOpenAd == null || !appOpenAd.CanShowAd())
  141. return false;
  142. double ageHours = (DateTime.UtcNow - appOpenLoadUtc).TotalHours;
  143. return ageHours < AdMobPolicy.AppOpenAdMaxAgeHours;
  144. }
  145. public static void ShowAppOpen(Action onClosed, Action<string> onFailed)
  146. {
  147. if (!CanShowAppOpen())
  148. {
  149. onFailed?.Invoke("CANNOT SHOW");
  150. return;
  151. }
  152. bool completed = false;
  153. appOpenAd.OnAdFullScreenContentClosed += () =>
  154. {
  155. completed = true;
  156. onClosed?.Invoke();
  157. };
  158. appOpenAd.OnAdFullScreenContentFailed += error =>
  159. {
  160. if (!completed)
  161. onFailed?.Invoke(error != null ? error.GetMessage() : "SHOW FAILED");
  162. };
  163. appOpenAd.Show();
  164. }
  165. public static void DestroyAppOpen()
  166. {
  167. if (appOpenAd == null)
  168. return;
  169. appOpenAd.Destroy();
  170. appOpenAd = null;
  171. }
  172. public static void ShowBottomBanner(string unitId)
  173. {
  174. HideBottomBanner();
  175. var adSize = AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(AdSize.FullWidth);
  176. bannerView = new BannerView(unitId, adSize, AdPosition.Bottom);
  177. bannerView.OnBannerAdLoadFailed += error =>
  178. {
  179. UnityEngine.Debug.LogWarning("[AdMob] Banner load failed: " + (error != null ? error.GetMessage() : "unknown"));
  180. };
  181. bannerView.LoadAd(new AdRequest());
  182. }
  183. public static void HideBottomBanner()
  184. {
  185. if (bannerView == null)
  186. return;
  187. bannerView.Destroy();
  188. bannerView = null;
  189. }
  190. static void RegisterFullScreenCallbacks(object ad)
  191. {
  192. switch (ad)
  193. {
  194. case RewardedAd rewarded:
  195. rewarded.OnAdFullScreenContentOpened += () => FullScreenAdOpened?.Invoke();
  196. rewarded.OnAdFullScreenContentClosed += () => FullScreenAdClosed?.Invoke();
  197. break;
  198. case InterstitialAd interstitial:
  199. interstitial.OnAdFullScreenContentOpened += () => FullScreenAdOpened?.Invoke();
  200. interstitial.OnAdFullScreenContentClosed += () => FullScreenAdClosed?.Invoke();
  201. break;
  202. case AppOpenAd appOpen:
  203. appOpen.OnAdFullScreenContentOpened += () => FullScreenAdOpened?.Invoke();
  204. appOpen.OnAdFullScreenContentClosed += () => FullScreenAdClosed?.Invoke();
  205. break;
  206. }
  207. }
  208. }
  209. #endif