#if CRAZY_FRUIT_ADMOB using System; using GoogleMobileAds.Api; /// /// Google Mobile Ads bridge. Enable with scripting define CRAZY_FRUIT_ADMOB. /// public static class AdMobGmaBridge { static RewardedAd rewardedAd; static InterstitialAd interstitialAd; static AppOpenAd appOpenAd; static DateTime appOpenLoadUtc; static BannerView bannerView; public static event Action FullScreenAdOpened; public static event Action FullScreenAdClosed; public static void Initialize(string androidAppId, string iosAppId, Action onComplete) { #if UNITY_IOS MobileAds.SetiOSAppPauseOnBackground(true); #endif MobileAds.Initialize(_ => { onComplete?.Invoke(); }); } public static void LoadRewarded(string unitId, Action onLoaded, Action onFailed) { if (rewardedAd != null) { rewardedAd.Destroy(); rewardedAd = null; } RewardedAd.Load(unitId, new AdRequest(), (ad, error) => { if (error != null || ad == null) { onFailed?.Invoke(error != null ? error.GetMessage() : "LOAD FAILED"); return; } rewardedAd = ad; RegisterFullScreenCallbacks(rewardedAd); onLoaded?.Invoke(); }); } public static void ShowRewarded(Action onReward, Action onFailed) { if (rewardedAd == null || !rewardedAd.CanShowAd()) { onFailed?.Invoke("CANNOT SHOW"); return; } bool rewarded = false; rewardedAd.OnAdFullScreenContentClosed += () => { if (!rewarded) onFailed?.Invoke("CLOSED EARLY"); }; rewardedAd.OnAdFullScreenContentFailed += error => { onFailed?.Invoke(error != null ? error.GetMessage() : "SHOW FAILED"); }; rewardedAd.Show(reward => { rewarded = true; onReward?.Invoke(); }); } public static void LoadInterstitial(string unitId, Action onLoaded, Action onFailed) { if (interstitialAd != null) { interstitialAd.Destroy(); interstitialAd = null; } InterstitialAd.Load(unitId, new AdRequest(), (ad, error) => { if (error != null || ad == null) { onFailed?.Invoke(error != null ? error.GetMessage() : "LOAD FAILED"); return; } interstitialAd = ad; RegisterFullScreenCallbacks(interstitialAd); onLoaded?.Invoke(); }); } public static bool CanShowInterstitial() { return interstitialAd != null && interstitialAd.CanShowAd(); } public static void ShowInterstitial(Action onClosed, Action onFailed) { if (!CanShowInterstitial()) { onFailed?.Invoke("CANNOT SHOW"); return; } bool completed = false; interstitialAd.OnAdFullScreenContentClosed += () => { completed = true; onClosed?.Invoke(); }; interstitialAd.OnAdFullScreenContentFailed += error => { if (!completed) onFailed?.Invoke(error != null ? error.GetMessage() : "SHOW FAILED"); }; interstitialAd.Show(); } public static void DestroyInterstitial() { if (interstitialAd == null) return; interstitialAd.Destroy(); interstitialAd = null; } public static void LoadAppOpen(string unitId, Action onLoaded, Action onFailed) { if (appOpenAd != null) { appOpenAd.Destroy(); appOpenAd = null; } AppOpenAd.Load(unitId, new AdRequest(), (ad, error) => { if (error != null || ad == null) { onFailed?.Invoke(error != null ? error.GetMessage() : "LOAD FAILED"); return; } appOpenAd = ad; appOpenLoadUtc = DateTime.UtcNow; RegisterFullScreenCallbacks(appOpenAd); onLoaded?.Invoke(); }); } public static bool CanShowAppOpen() { if (appOpenAd == null || !appOpenAd.CanShowAd()) return false; double ageHours = (DateTime.UtcNow - appOpenLoadUtc).TotalHours; return ageHours < AdMobPolicy.AppOpenAdMaxAgeHours; } public static void ShowAppOpen(Action onClosed, Action onFailed) { if (!CanShowAppOpen()) { onFailed?.Invoke("CANNOT SHOW"); return; } bool completed = false; appOpenAd.OnAdFullScreenContentClosed += () => { completed = true; onClosed?.Invoke(); }; appOpenAd.OnAdFullScreenContentFailed += error => { if (!completed) onFailed?.Invoke(error != null ? error.GetMessage() : "SHOW FAILED"); }; appOpenAd.Show(); } public static void DestroyAppOpen() { if (appOpenAd == null) return; appOpenAd.Destroy(); appOpenAd = null; } public static void ShowBottomBanner(string unitId) { HideBottomBanner(); var adSize = AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(AdSize.FullWidth); bannerView = new BannerView(unitId, adSize, AdPosition.Bottom); bannerView.OnBannerAdLoadFailed += error => { UnityEngine.Debug.LogWarning("[AdMob] Banner load failed: " + (error != null ? error.GetMessage() : "unknown")); }; bannerView.LoadAd(new AdRequest()); } public static void HideBottomBanner() { if (bannerView == null) return; bannerView.Destroy(); bannerView = null; } static void RegisterFullScreenCallbacks(object ad) { switch (ad) { case RewardedAd rewarded: rewarded.OnAdFullScreenContentOpened += () => FullScreenAdOpened?.Invoke(); rewarded.OnAdFullScreenContentClosed += () => FullScreenAdClosed?.Invoke(); break; case InterstitialAd interstitial: interstitial.OnAdFullScreenContentOpened += () => FullScreenAdOpened?.Invoke(); interstitial.OnAdFullScreenContentClosed += () => FullScreenAdClosed?.Invoke(); break; case AppOpenAd appOpen: appOpen.OnAdFullScreenContentOpened += () => FullScreenAdOpened?.Invoke(); appOpen.OnAdFullScreenContentClosed += () => FullScreenAdClosed?.Invoke(); break; } } } #endif