| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [DisallowMultipleComponent]
- public class AdMobService : MonoBehaviour
- {
- public const bool EnableBottomBanner = false;
- static AdMobService instance;
- bool initialized;
- bool rewardedReady;
- bool rewardedLoading;
- bool interstitialReady;
- bool interstitialLoading;
- bool appOpenReady;
- bool appOpenLoading;
- bool fullscreenAdShowing;
- bool bannerVisible;
- bool bannerRequested;
- bool adsLoadAllowed;
- bool suppressAppOpenOnce;
- bool initInFlight;
- AdPlacement loadingPlacement;
- Coroutine showRoutine;
- List<Action> initWaiters;
- public static bool IsRewardedReady(AdPlacement placement)
- {
- if (instance == null)
- return false;
- return instance.rewardedReady && instance.loadingPlacement == placement;
- }
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
- static void Bootstrap()
- {
- Ensure();
- }
- static AdMobService Ensure()
- {
- if (!Application.isPlaying)
- return null;
- if (instance != null)
- return instance;
- var go = new GameObject("AdMobService");
- DontDestroyOnLoad(go);
- return go.AddComponent<AdMobService>();
- }
- void Awake()
- {
- if (instance != null && instance != this)
- {
- Destroy(gameObject);
- return;
- }
- instance = this;
- }
- void OnEnable()
- {
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- AdMobGmaBridge.FullScreenAdOpened += HandleFullScreenAdOpened;
- AdMobGmaBridge.FullScreenAdClosed += HandleFullScreenAdClosed;
- #endif
- }
- void OnDisable()
- {
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- AdMobGmaBridge.FullScreenAdOpened -= HandleFullScreenAdOpened;
- AdMobGmaBridge.FullScreenAdClosed -= HandleFullScreenAdClosed;
- #endif
- }
- void OnDestroy()
- {
- if (instance == this)
- instance = null;
- }
- void OnApplicationPause(bool paused)
- {
- if (paused || !initialized)
- return;
- if (fullscreenAdShowing || suppressAppOpenOnce)
- {
- suppressAppOpenOnce = false;
- return;
- }
- if (!AdMobPolicy.CanShowAppOpen())
- return;
- TryShowAppOpenLaunch(null);
- }
- void HandleFullScreenAdOpened()
- {
- fullscreenAdShowing = true;
- HideBottomBannerInternal();
- }
- void HandleFullScreenAdClosed()
- {
- fullscreenAdShowing = false;
- if (bannerRequested)
- ShowBottomBannerInternal();
- }
- public static void Initialize(Action onComplete = null)
- {
- var service = Ensure();
- if (service == null)
- {
- onComplete?.Invoke();
- return;
- }
- service.EnsureInitialized(onComplete);
- }
- public static void PreloadRewarded(AdPlacement placement)
- {
- if (instance == null)
- Ensure();
- if (instance != null)
- instance.LoadRewarded(placement);
- }
- public static void PreloadLevelAds()
- {
- if (instance == null)
- Ensure();
- if (instance == null)
- return;
- instance.LoadRewarded(AdPlacement.Revive);
- if (AdMobEngagement.NonRewardedAdsEnabled)
- {
- instance.LoadInterstitial();
- instance.LoadAppOpen();
- }
- }
- public static void NotifyNonRewardedAdsUnlocked()
- {
- if (instance == null)
- Ensure();
- if (instance == null || !AdMobEngagement.NonRewardedAdsEnabled)
- return;
- instance.EnsureInitialized(() =>
- {
- instance.LoadInterstitial();
- instance.LoadAppOpen();
- instance.ShowBottomBannerInternal();
- });
- }
- public static void ShowRewarded(AdPlacement placement, Action onReward, Action<string> onFailed)
- {
- if (instance == null)
- Ensure();
- if (instance == null)
- {
- onFailed?.Invoke("AD SERVICE UNAVAILABLE");
- return;
- }
- instance.ShowRewardedInternal(placement, onReward, onFailed);
- }
- public static void TryShowInterstitialLevel(Action onFinished)
- {
- if (instance == null)
- Ensure();
- if (instance == null)
- {
- onFinished?.Invoke();
- return;
- }
- instance.TryShowInterstitialInternal(onFinished);
- }
- public static void TryShowAppOpenLaunch(Action onFinished)
- {
- if (instance == null)
- Ensure();
- if (instance == null)
- {
- onFinished?.Invoke();
- return;
- }
- instance.TryShowAppOpenInternal(onFinished);
- }
- public static void ShowBottomBanner()
- {
- if (!EnableBottomBanner)
- return;
- if (instance == null)
- Ensure();
- if (instance == null)
- return;
- instance.bannerRequested = true;
- if (!instance.initialized)
- {
- instance.EnsureInitialized(() => instance.ShowBottomBannerInternal());
- return;
- }
- instance.ShowBottomBannerInternal();
- }
- public static void HideBottomBanner()
- {
- if (instance == null)
- return;
- instance.bannerRequested = false;
- instance.HideBottomBannerInternal();
- }
- public static void SuppressNextForegroundAppOpen()
- {
- if (instance == null)
- return;
- instance.suppressAppOpenOnce = true;
- }
- void EnsureInitialized(Action onComplete)
- {
- if (initialized)
- {
- onComplete?.Invoke();
- return;
- }
- initWaiters ??= new List<Action>();
- if (onComplete != null)
- initWaiters.Add(onComplete);
- if (initInFlight)
- return;
- initInFlight = true;
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- AdMobConsentBridge.RunConsentGate(canRequestAds =>
- {
- adsLoadAllowed = canRequestAds;
- if (!canRequestAds)
- {
- Debug.LogWarning("[AdMobService] Consent withheld — ads will not load.");
- FinishInitialization();
- return;
- }
- AdMobGmaBridge.Initialize(AdMobIds.AndroidAppId, AdMobIds.IosAppId, () =>
- {
- PreloadLevelAds();
- FinishInitialization();
- });
- });
- #else
- adsLoadAllowed = true;
- Debug.Log("[AdMobService] Using stub ads (Editor or CRAZY_FRUIT_ADMOB not defined).");
- FinishInitialization();
- #endif
- }
- void FinishInitialization()
- {
- initialized = true;
- initInFlight = false;
- if (bannerRequested)
- ShowBottomBannerInternal();
- if (initWaiters == null || initWaiters.Count == 0)
- return;
- var waiters = initWaiters.ToArray();
- initWaiters.Clear();
- for (int i = 0; i < waiters.Length; i++)
- waiters[i]?.Invoke();
- }
- bool CanLoadAds()
- {
- return adsLoadAllowed;
- }
- static bool NonRewardedAdsAllowed()
- {
- return AdMobEngagement.NonRewardedAdsEnabled;
- }
- void LoadRewarded(AdPlacement placement)
- {
- if (!initialized)
- {
- EnsureInitialized(() => LoadRewarded(placement));
- return;
- }
- if (!CanLoadAds())
- return;
- if (rewardedLoading)
- return;
- if (rewardedReady && loadingPlacement == placement)
- return;
- rewardedLoading = true;
- rewardedReady = false;
- loadingPlacement = placement;
- string unitId = AdMobIds.RewardedUnitId(placement);
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- AdMobGmaBridge.LoadRewarded(unitId, OnRewardedLoaded, OnRewardedLoadFailed);
- #else
- StartCoroutine(StubLoadRoutine(() =>
- {
- rewardedLoading = false;
- rewardedReady = true;
- }));
- #endif
- }
- void LoadInterstitial()
- {
- if (!initialized)
- {
- EnsureInitialized(LoadInterstitial);
- return;
- }
- if (!CanLoadAds() || !NonRewardedAdsAllowed())
- return;
- if (interstitialLoading)
- return;
- if (interstitialReady)
- return;
- interstitialLoading = true;
- interstitialReady = false;
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- AdMobGmaBridge.LoadInterstitial(
- AdMobIds.InterstitialLevelUnitId(),
- OnInterstitialLoaded,
- OnInterstitialLoadFailed);
- #else
- StartCoroutine(StubLoadRoutine(() =>
- {
- interstitialLoading = false;
- interstitialReady = true;
- }));
- #endif
- }
- void LoadAppOpen()
- {
- if (!initialized)
- {
- EnsureInitialized(LoadAppOpen);
- return;
- }
- if (!CanLoadAds() || !NonRewardedAdsAllowed())
- return;
- if (appOpenLoading)
- return;
- if (appOpenReady)
- return;
- appOpenLoading = true;
- appOpenReady = false;
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- AdMobGmaBridge.LoadAppOpen(
- AdMobIds.AppOpenLaunchUnitId(),
- OnAppOpenLoaded,
- OnAppOpenLoadFailed);
- #else
- StartCoroutine(StubLoadRoutine(() =>
- {
- appOpenLoading = false;
- appOpenReady = true;
- }));
- #endif
- }
- IEnumerator StubLoadRoutine(Action onDone)
- {
- yield return new WaitForSecondsRealtime(0.35f);
- onDone?.Invoke();
- }
- void OnRewardedLoaded()
- {
- rewardedLoading = false;
- rewardedReady = true;
- }
- void OnRewardedLoadFailed(string message)
- {
- rewardedLoading = false;
- rewardedReady = false;
- Debug.LogWarning("[AdMobService] Rewarded load failed: " + message);
- }
- void OnInterstitialLoaded()
- {
- interstitialLoading = false;
- interstitialReady = true;
- }
- void OnInterstitialLoadFailed(string message)
- {
- interstitialLoading = false;
- interstitialReady = false;
- Debug.LogWarning("[AdMobService] Interstitial load failed: " + message);
- }
- void OnAppOpenLoaded()
- {
- appOpenLoading = false;
- appOpenReady = true;
- }
- void OnAppOpenLoadFailed(string message)
- {
- appOpenLoading = false;
- appOpenReady = false;
- Debug.LogWarning("[AdMobService] App open load failed: " + message);
- }
- void ShowRewardedInternal(AdPlacement placement, Action onReward, Action<string> onFailed)
- {
- if (showRoutine != null)
- {
- onFailed?.Invoke("AD ALREADY PLAYING");
- return;
- }
- if (!CanLoadAds())
- {
- onFailed?.Invoke("ADS DISABLED");
- return;
- }
- if (!rewardedReady || loadingPlacement != placement)
- {
- LoadRewarded(placement);
- onFailed?.Invoke("AD NOT READY");
- return;
- }
- suppressAppOpenOnce = true;
- showRoutine = StartCoroutine(ShowRewardedRoutine(placement, onReward, onFailed));
- }
- IEnumerator ShowRewardedRoutine(AdPlacement placement, Action onReward, Action<string> onFailed)
- {
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- bool finished = false;
- string failReason = "";
- AdMobGmaBridge.ShowRewarded(
- () =>
- {
- finished = true;
- onReward?.Invoke();
- },
- reason =>
- {
- finished = true;
- failReason = reason;
- });
- while (!finished)
- yield return null;
- showRoutine = null;
- rewardedReady = false;
- LoadRewarded(placement);
- if (!string.IsNullOrEmpty(failReason))
- onFailed?.Invoke(failReason);
- #else
- yield return new WaitForSecondsRealtime(0.6f);
- showRoutine = null;
- rewardedReady = false;
- onReward?.Invoke();
- LoadRewarded(placement);
- #endif
- }
- void TryShowInterstitialInternal(Action onFinished)
- {
- if (!initialized)
- {
- EnsureInitialized(() => TryShowInterstitialInternal(onFinished));
- return;
- }
- if (showRoutine != null)
- {
- onFinished?.Invoke();
- return;
- }
- if (!CanLoadAds() || !NonRewardedAdsAllowed())
- {
- onFinished?.Invoke();
- return;
- }
- if (!AdMobPolicy.CanShowInterstitial())
- {
- onFinished?.Invoke();
- return;
- }
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- if (!interstitialReady || !AdMobGmaBridge.CanShowInterstitial())
- {
- LoadInterstitial();
- onFinished?.Invoke();
- return;
- }
- suppressAppOpenOnce = true;
- showRoutine = StartCoroutine(ShowInterstitialRoutine(onFinished));
- #else
- if (!interstitialReady)
- {
- LoadInterstitial();
- onFinished?.Invoke();
- return;
- }
- showRoutine = StartCoroutine(StubFullScreenRoutine(() =>
- {
- AdMobPolicy.MarkInterstitialShown();
- interstitialReady = false;
- LoadInterstitial();
- onFinished?.Invoke();
- }));
- #endif
- }
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- IEnumerator ShowInterstitialRoutine(Action onFinished)
- {
- bool finished = false;
- AdMobGmaBridge.ShowInterstitial(
- () =>
- {
- finished = true;
- AdMobPolicy.MarkInterstitialShown();
- interstitialReady = false;
- AdMobGmaBridge.DestroyInterstitial();
- LoadInterstitial();
- onFinished?.Invoke();
- },
- _ =>
- {
- if (finished)
- return;
- finished = true;
- interstitialReady = false;
- AdMobGmaBridge.DestroyInterstitial();
- LoadInterstitial();
- onFinished?.Invoke();
- });
- while (!finished)
- yield return null;
- showRoutine = null;
- }
- #endif
- IEnumerator StubFullScreenRoutine(Action onClosed)
- {
- yield return new WaitForSecondsRealtime(0.5f);
- showRoutine = null;
- onClosed?.Invoke();
- }
- void TryShowAppOpenInternal(Action onFinished)
- {
- if (!initialized)
- {
- EnsureInitialized(() => TryShowAppOpenInternal(onFinished));
- return;
- }
- if (showRoutine != null)
- {
- onFinished?.Invoke();
- return;
- }
- if (!CanLoadAds() || !NonRewardedAdsAllowed())
- {
- onFinished?.Invoke();
- return;
- }
- if (!AdMobPolicy.CanShowAppOpen())
- {
- onFinished?.Invoke();
- return;
- }
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- if (!appOpenReady || !AdMobGmaBridge.CanShowAppOpen())
- {
- LoadAppOpen();
- onFinished?.Invoke();
- return;
- }
- showRoutine = StartCoroutine(ShowAppOpenRoutine(onFinished));
- #else
- if (!appOpenReady)
- {
- LoadAppOpen();
- onFinished?.Invoke();
- return;
- }
- showRoutine = StartCoroutine(StubFullScreenRoutine(() =>
- {
- AdMobPolicy.MarkAppOpenShown();
- appOpenReady = false;
- LoadAppOpen();
- onFinished?.Invoke();
- }));
- #endif
- }
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- IEnumerator ShowAppOpenRoutine(Action onFinished)
- {
- bool finished = false;
- AdMobGmaBridge.ShowAppOpen(
- () =>
- {
- finished = true;
- AdMobPolicy.MarkAppOpenShown();
- appOpenReady = false;
- AdMobGmaBridge.DestroyAppOpen();
- LoadAppOpen();
- onFinished?.Invoke();
- },
- _ =>
- {
- if (finished)
- return;
- finished = true;
- appOpenReady = false;
- AdMobGmaBridge.DestroyAppOpen();
- LoadAppOpen();
- onFinished?.Invoke();
- });
- while (!finished)
- yield return null;
- showRoutine = null;
- }
- #endif
- void ShowBottomBannerInternal()
- {
- if (!EnableBottomBanner || !bannerRequested || bannerVisible || fullscreenAdShowing || !CanLoadAds() || !NonRewardedAdsAllowed())
- return;
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- AdMobGmaBridge.ShowBottomBanner(AdMobIds.BannerBottomUnitId());
- bannerVisible = true;
- #else
- bannerVisible = true;
- Debug.Log("[AdMobService] Stub bottom banner shown.");
- #endif
- }
- void HideBottomBannerInternal()
- {
- if (!bannerVisible)
- return;
- #if CRAZY_FRUIT_ADMOB && !UNITY_EDITOR
- AdMobGmaBridge.HideBottomBanner();
- #endif
- bannerVisible = false;
- }
- }
|