| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- #if CRAZY_FRUIT_ADMOB
- using System;
- using GoogleMobileAds.Api;
- /// <summary>
- /// Google Mobile Ads bridge. Enable with scripting define CRAZY_FRUIT_ADMOB.
- /// </summary>
- 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<string> 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<string> 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<string> 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<string> 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<string> 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<string> 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
|