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 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(); } 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 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(); 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 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 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; } }