AdMobService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. [DisallowMultipleComponent]
  6. public class AdMobService : MonoBehaviour
  7. {
  8. public const bool EnableBottomBanner = false;
  9. static AdMobService instance;
  10. bool initialized;
  11. bool rewardedReady;
  12. bool rewardedLoading;
  13. bool interstitialReady;
  14. bool interstitialLoading;
  15. bool appOpenReady;
  16. bool appOpenLoading;
  17. bool fullscreenAdShowing;
  18. bool bannerVisible;
  19. bool bannerRequested;
  20. bool adsLoadAllowed;
  21. bool suppressAppOpenOnce;
  22. bool initInFlight;
  23. AdPlacement loadingPlacement;
  24. Coroutine showRoutine;
  25. List<Action> initWaiters;
  26. public static bool IsRewardedReady(AdPlacement placement)
  27. {
  28. if (instance == null)
  29. return false;
  30. return instance.rewardedReady && instance.loadingPlacement == placement;
  31. }
  32. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  33. static void Bootstrap()
  34. {
  35. Ensure();
  36. }
  37. static AdMobService Ensure()
  38. {
  39. if (!Application.isPlaying)
  40. return null;
  41. if (instance != null)
  42. return instance;
  43. var go = new GameObject("AdMobService");
  44. DontDestroyOnLoad(go);
  45. return go.AddComponent<AdMobService>();
  46. }
  47. void Awake()
  48. {
  49. if (instance != null && instance != this)
  50. {
  51. Destroy(gameObject);
  52. return;
  53. }
  54. instance = this;
  55. }
  56. void OnEnable()
  57. {
  58. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  59. AdMobGmaBridge.FullScreenAdOpened += HandleFullScreenAdOpened;
  60. AdMobGmaBridge.FullScreenAdClosed += HandleFullScreenAdClosed;
  61. #endif
  62. }
  63. void OnDisable()
  64. {
  65. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  66. AdMobGmaBridge.FullScreenAdOpened -= HandleFullScreenAdOpened;
  67. AdMobGmaBridge.FullScreenAdClosed -= HandleFullScreenAdClosed;
  68. #endif
  69. }
  70. void OnDestroy()
  71. {
  72. if (instance == this)
  73. instance = null;
  74. }
  75. void OnApplicationPause(bool paused)
  76. {
  77. if (paused || !initialized)
  78. return;
  79. if (fullscreenAdShowing || suppressAppOpenOnce)
  80. {
  81. suppressAppOpenOnce = false;
  82. return;
  83. }
  84. if (!AdMobPolicy.CanShowAppOpen())
  85. return;
  86. TryShowAppOpenLaunch(null);
  87. }
  88. void HandleFullScreenAdOpened()
  89. {
  90. fullscreenAdShowing = true;
  91. HideBottomBannerInternal();
  92. }
  93. void HandleFullScreenAdClosed()
  94. {
  95. fullscreenAdShowing = false;
  96. if (bannerRequested)
  97. ShowBottomBannerInternal();
  98. }
  99. public static void Initialize(Action onComplete = null)
  100. {
  101. var service = Ensure();
  102. if (service == null)
  103. {
  104. onComplete?.Invoke();
  105. return;
  106. }
  107. service.EnsureInitialized(onComplete);
  108. }
  109. public static void PreloadRewarded(AdPlacement placement)
  110. {
  111. if (instance == null)
  112. Ensure();
  113. if (instance != null)
  114. instance.LoadRewarded(placement);
  115. }
  116. public static void PreloadLevelAds()
  117. {
  118. if (instance == null)
  119. Ensure();
  120. if (instance == null)
  121. return;
  122. instance.LoadRewarded(AdPlacement.Revive);
  123. if (AdMobEngagement.NonRewardedAdsEnabled)
  124. {
  125. instance.LoadInterstitial();
  126. instance.LoadAppOpen();
  127. }
  128. }
  129. public static void NotifyNonRewardedAdsUnlocked()
  130. {
  131. if (instance == null)
  132. Ensure();
  133. if (instance == null || !AdMobEngagement.NonRewardedAdsEnabled)
  134. return;
  135. instance.EnsureInitialized(() =>
  136. {
  137. instance.LoadInterstitial();
  138. instance.LoadAppOpen();
  139. instance.ShowBottomBannerInternal();
  140. });
  141. }
  142. public static void ShowRewarded(AdPlacement placement, Action onReward, Action<string> onFailed)
  143. {
  144. if (instance == null)
  145. Ensure();
  146. if (instance == null)
  147. {
  148. onFailed?.Invoke("AD SERVICE UNAVAILABLE");
  149. return;
  150. }
  151. instance.ShowRewardedInternal(placement, onReward, onFailed);
  152. }
  153. public static void TryShowInterstitialLevel(Action onFinished)
  154. {
  155. if (instance == null)
  156. Ensure();
  157. if (instance == null)
  158. {
  159. onFinished?.Invoke();
  160. return;
  161. }
  162. instance.TryShowInterstitialInternal(onFinished);
  163. }
  164. public static void TryShowAppOpenLaunch(Action onFinished)
  165. {
  166. if (instance == null)
  167. Ensure();
  168. if (instance == null)
  169. {
  170. onFinished?.Invoke();
  171. return;
  172. }
  173. instance.TryShowAppOpenInternal(onFinished);
  174. }
  175. public static void ShowBottomBanner()
  176. {
  177. if (!EnableBottomBanner)
  178. return;
  179. if (instance == null)
  180. Ensure();
  181. if (instance == null)
  182. return;
  183. instance.bannerRequested = true;
  184. if (!instance.initialized)
  185. {
  186. instance.EnsureInitialized(() => instance.ShowBottomBannerInternal());
  187. return;
  188. }
  189. instance.ShowBottomBannerInternal();
  190. }
  191. public static void HideBottomBanner()
  192. {
  193. if (instance == null)
  194. return;
  195. instance.bannerRequested = false;
  196. instance.HideBottomBannerInternal();
  197. }
  198. public static void SuppressNextForegroundAppOpen()
  199. {
  200. if (instance == null)
  201. return;
  202. instance.suppressAppOpenOnce = true;
  203. }
  204. void EnsureInitialized(Action onComplete)
  205. {
  206. if (initialized)
  207. {
  208. onComplete?.Invoke();
  209. return;
  210. }
  211. initWaiters ??= new List<Action>();
  212. if (onComplete != null)
  213. initWaiters.Add(onComplete);
  214. if (initInFlight)
  215. return;
  216. initInFlight = true;
  217. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  218. AdMobConsentBridge.RunConsentGate(canRequestAds =>
  219. {
  220. adsLoadAllowed = canRequestAds;
  221. if (!canRequestAds)
  222. {
  223. Debug.LogWarning("[AdMobService] Consent withheld — ads will not load.");
  224. FinishInitialization();
  225. return;
  226. }
  227. AdMobGmaBridge.Initialize(AdMobIds.AndroidAppId, AdMobIds.IosAppId, () =>
  228. {
  229. PreloadLevelAds();
  230. FinishInitialization();
  231. });
  232. });
  233. #else
  234. adsLoadAllowed = true;
  235. Debug.Log("[AdMobService] Using stub ads (Editor or CRAZY_FRUIT_ADMOB not defined).");
  236. FinishInitialization();
  237. #endif
  238. }
  239. void FinishInitialization()
  240. {
  241. initialized = true;
  242. initInFlight = false;
  243. if (bannerRequested)
  244. ShowBottomBannerInternal();
  245. if (initWaiters == null || initWaiters.Count == 0)
  246. return;
  247. var waiters = initWaiters.ToArray();
  248. initWaiters.Clear();
  249. for (int i = 0; i < waiters.Length; i++)
  250. waiters[i]?.Invoke();
  251. }
  252. bool CanLoadAds()
  253. {
  254. return adsLoadAllowed;
  255. }
  256. static bool NonRewardedAdsAllowed()
  257. {
  258. return AdMobEngagement.NonRewardedAdsEnabled;
  259. }
  260. void LoadRewarded(AdPlacement placement)
  261. {
  262. if (!initialized)
  263. {
  264. EnsureInitialized(() => LoadRewarded(placement));
  265. return;
  266. }
  267. if (!CanLoadAds())
  268. return;
  269. if (rewardedLoading)
  270. return;
  271. if (rewardedReady && loadingPlacement == placement)
  272. return;
  273. rewardedLoading = true;
  274. rewardedReady = false;
  275. loadingPlacement = placement;
  276. string unitId = AdMobIds.RewardedUnitId(placement);
  277. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  278. AdMobGmaBridge.LoadRewarded(unitId, OnRewardedLoaded, OnRewardedLoadFailed);
  279. #else
  280. StartCoroutine(StubLoadRoutine(() =>
  281. {
  282. rewardedLoading = false;
  283. rewardedReady = true;
  284. }));
  285. #endif
  286. }
  287. void LoadInterstitial()
  288. {
  289. if (!initialized)
  290. {
  291. EnsureInitialized(LoadInterstitial);
  292. return;
  293. }
  294. if (!CanLoadAds() || !NonRewardedAdsAllowed())
  295. return;
  296. if (interstitialLoading)
  297. return;
  298. if (interstitialReady)
  299. return;
  300. interstitialLoading = true;
  301. interstitialReady = false;
  302. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  303. AdMobGmaBridge.LoadInterstitial(
  304. AdMobIds.InterstitialLevelUnitId(),
  305. OnInterstitialLoaded,
  306. OnInterstitialLoadFailed);
  307. #else
  308. StartCoroutine(StubLoadRoutine(() =>
  309. {
  310. interstitialLoading = false;
  311. interstitialReady = true;
  312. }));
  313. #endif
  314. }
  315. void LoadAppOpen()
  316. {
  317. if (!initialized)
  318. {
  319. EnsureInitialized(LoadAppOpen);
  320. return;
  321. }
  322. if (!CanLoadAds() || !NonRewardedAdsAllowed())
  323. return;
  324. if (appOpenLoading)
  325. return;
  326. if (appOpenReady)
  327. return;
  328. appOpenLoading = true;
  329. appOpenReady = false;
  330. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  331. AdMobGmaBridge.LoadAppOpen(
  332. AdMobIds.AppOpenLaunchUnitId(),
  333. OnAppOpenLoaded,
  334. OnAppOpenLoadFailed);
  335. #else
  336. StartCoroutine(StubLoadRoutine(() =>
  337. {
  338. appOpenLoading = false;
  339. appOpenReady = true;
  340. }));
  341. #endif
  342. }
  343. IEnumerator StubLoadRoutine(Action onDone)
  344. {
  345. yield return new WaitForSecondsRealtime(0.35f);
  346. onDone?.Invoke();
  347. }
  348. void OnRewardedLoaded()
  349. {
  350. rewardedLoading = false;
  351. rewardedReady = true;
  352. }
  353. void OnRewardedLoadFailed(string message)
  354. {
  355. rewardedLoading = false;
  356. rewardedReady = false;
  357. Debug.LogWarning("[AdMobService] Rewarded load failed: " + message);
  358. }
  359. void OnInterstitialLoaded()
  360. {
  361. interstitialLoading = false;
  362. interstitialReady = true;
  363. }
  364. void OnInterstitialLoadFailed(string message)
  365. {
  366. interstitialLoading = false;
  367. interstitialReady = false;
  368. Debug.LogWarning("[AdMobService] Interstitial load failed: " + message);
  369. }
  370. void OnAppOpenLoaded()
  371. {
  372. appOpenLoading = false;
  373. appOpenReady = true;
  374. }
  375. void OnAppOpenLoadFailed(string message)
  376. {
  377. appOpenLoading = false;
  378. appOpenReady = false;
  379. Debug.LogWarning("[AdMobService] App open load failed: " + message);
  380. }
  381. void ShowRewardedInternal(AdPlacement placement, Action onReward, Action<string> onFailed)
  382. {
  383. if (showRoutine != null)
  384. {
  385. onFailed?.Invoke("AD ALREADY PLAYING");
  386. return;
  387. }
  388. if (!CanLoadAds())
  389. {
  390. onFailed?.Invoke("ADS DISABLED");
  391. return;
  392. }
  393. if (!rewardedReady || loadingPlacement != placement)
  394. {
  395. LoadRewarded(placement);
  396. onFailed?.Invoke("AD NOT READY");
  397. return;
  398. }
  399. suppressAppOpenOnce = true;
  400. showRoutine = StartCoroutine(ShowRewardedRoutine(placement, onReward, onFailed));
  401. }
  402. IEnumerator ShowRewardedRoutine(AdPlacement placement, Action onReward, Action<string> onFailed)
  403. {
  404. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  405. bool finished = false;
  406. string failReason = "";
  407. AdMobGmaBridge.ShowRewarded(
  408. () =>
  409. {
  410. finished = true;
  411. onReward?.Invoke();
  412. },
  413. reason =>
  414. {
  415. finished = true;
  416. failReason = reason;
  417. });
  418. while (!finished)
  419. yield return null;
  420. showRoutine = null;
  421. rewardedReady = false;
  422. LoadRewarded(placement);
  423. if (!string.IsNullOrEmpty(failReason))
  424. onFailed?.Invoke(failReason);
  425. #else
  426. yield return new WaitForSecondsRealtime(0.6f);
  427. showRoutine = null;
  428. rewardedReady = false;
  429. onReward?.Invoke();
  430. LoadRewarded(placement);
  431. #endif
  432. }
  433. void TryShowInterstitialInternal(Action onFinished)
  434. {
  435. if (!initialized)
  436. {
  437. EnsureInitialized(() => TryShowInterstitialInternal(onFinished));
  438. return;
  439. }
  440. if (showRoutine != null)
  441. {
  442. onFinished?.Invoke();
  443. return;
  444. }
  445. if (!CanLoadAds() || !NonRewardedAdsAllowed())
  446. {
  447. onFinished?.Invoke();
  448. return;
  449. }
  450. if (!AdMobPolicy.CanShowInterstitial())
  451. {
  452. onFinished?.Invoke();
  453. return;
  454. }
  455. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  456. if (!interstitialReady || !AdMobGmaBridge.CanShowInterstitial())
  457. {
  458. LoadInterstitial();
  459. onFinished?.Invoke();
  460. return;
  461. }
  462. suppressAppOpenOnce = true;
  463. showRoutine = StartCoroutine(ShowInterstitialRoutine(onFinished));
  464. #else
  465. if (!interstitialReady)
  466. {
  467. LoadInterstitial();
  468. onFinished?.Invoke();
  469. return;
  470. }
  471. showRoutine = StartCoroutine(StubFullScreenRoutine(() =>
  472. {
  473. AdMobPolicy.MarkInterstitialShown();
  474. interstitialReady = false;
  475. LoadInterstitial();
  476. onFinished?.Invoke();
  477. }));
  478. #endif
  479. }
  480. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  481. IEnumerator ShowInterstitialRoutine(Action onFinished)
  482. {
  483. bool finished = false;
  484. AdMobGmaBridge.ShowInterstitial(
  485. () =>
  486. {
  487. finished = true;
  488. AdMobPolicy.MarkInterstitialShown();
  489. interstitialReady = false;
  490. AdMobGmaBridge.DestroyInterstitial();
  491. LoadInterstitial();
  492. onFinished?.Invoke();
  493. },
  494. _ =>
  495. {
  496. if (finished)
  497. return;
  498. finished = true;
  499. interstitialReady = false;
  500. AdMobGmaBridge.DestroyInterstitial();
  501. LoadInterstitial();
  502. onFinished?.Invoke();
  503. });
  504. while (!finished)
  505. yield return null;
  506. showRoutine = null;
  507. }
  508. #endif
  509. IEnumerator StubFullScreenRoutine(Action onClosed)
  510. {
  511. yield return new WaitForSecondsRealtime(0.5f);
  512. showRoutine = null;
  513. onClosed?.Invoke();
  514. }
  515. void TryShowAppOpenInternal(Action onFinished)
  516. {
  517. if (!initialized)
  518. {
  519. EnsureInitialized(() => TryShowAppOpenInternal(onFinished));
  520. return;
  521. }
  522. if (showRoutine != null)
  523. {
  524. onFinished?.Invoke();
  525. return;
  526. }
  527. if (!CanLoadAds() || !NonRewardedAdsAllowed())
  528. {
  529. onFinished?.Invoke();
  530. return;
  531. }
  532. if (!AdMobPolicy.CanShowAppOpen())
  533. {
  534. onFinished?.Invoke();
  535. return;
  536. }
  537. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  538. if (!appOpenReady || !AdMobGmaBridge.CanShowAppOpen())
  539. {
  540. LoadAppOpen();
  541. onFinished?.Invoke();
  542. return;
  543. }
  544. showRoutine = StartCoroutine(ShowAppOpenRoutine(onFinished));
  545. #else
  546. if (!appOpenReady)
  547. {
  548. LoadAppOpen();
  549. onFinished?.Invoke();
  550. return;
  551. }
  552. showRoutine = StartCoroutine(StubFullScreenRoutine(() =>
  553. {
  554. AdMobPolicy.MarkAppOpenShown();
  555. appOpenReady = false;
  556. LoadAppOpen();
  557. onFinished?.Invoke();
  558. }));
  559. #endif
  560. }
  561. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  562. IEnumerator ShowAppOpenRoutine(Action onFinished)
  563. {
  564. bool finished = false;
  565. AdMobGmaBridge.ShowAppOpen(
  566. () =>
  567. {
  568. finished = true;
  569. AdMobPolicy.MarkAppOpenShown();
  570. appOpenReady = false;
  571. AdMobGmaBridge.DestroyAppOpen();
  572. LoadAppOpen();
  573. onFinished?.Invoke();
  574. },
  575. _ =>
  576. {
  577. if (finished)
  578. return;
  579. finished = true;
  580. appOpenReady = false;
  581. AdMobGmaBridge.DestroyAppOpen();
  582. LoadAppOpen();
  583. onFinished?.Invoke();
  584. });
  585. while (!finished)
  586. yield return null;
  587. showRoutine = null;
  588. }
  589. #endif
  590. void ShowBottomBannerInternal()
  591. {
  592. if (!EnableBottomBanner || !bannerRequested || bannerVisible || fullscreenAdShowing || !CanLoadAds() || !NonRewardedAdsAllowed())
  593. return;
  594. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  595. AdMobGmaBridge.ShowBottomBanner(AdMobIds.BannerBottomUnitId());
  596. bannerVisible = true;
  597. #else
  598. bannerVisible = true;
  599. Debug.Log("[AdMobService] Stub bottom banner shown.");
  600. #endif
  601. }
  602. void HideBottomBannerInternal()
  603. {
  604. if (!bannerVisible)
  605. return;
  606. #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
  607. AdMobGmaBridge.HideBottomBanner();
  608. #endif
  609. bannerVisible = false;
  610. }
  611. }