alexwohlbruck / cat-facts

Daily cat facts! 🐱
https://cat-fact.herokuapp.com
Apache License 2.0
463 stars 80 forks source link

@NVentimiglia #181

Closed hau99 closed 1 year ago

hau99 commented 1 year ago

@NVentimiglia

Sure, see the code below. I only destroy and create the ads as done in this guide, but we need to avoid that?

private void RequestBannerAd()
    {
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-xxxx";
        if (_isDebugBuild)
        {
            adUnitId = "ca-app-pub-3940256099942544/6300978111"; //Fake
            PrintStatus("Using fake banner ad");
        }
#elif (UNITY_5 && UNITY_IOS) || UNITY_IPHONE
        string adUnitId = "ca-app-pub-xxxxx";
#else
        string adUnitId = "unexpected_platform";
#endif

        // Clean up banner before reusing
        if (_bannerView != null)
        {
            _bannerView.Destroy();
        }

        _bannerView = new BannerView(adUnitId, AdSize.GetLandscapeAnchoredAdaptiveBannerAdSizeWithWidth(450), AdPosition.Top);

        _bannerView.OnBannerAdLoaded += () =>
        {
            PrintStatus("Banner ad loaded.");
            OnAdLoadedEvent.Invoke();
        };

        _bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
        {
            PrintStatus("Banner ad failed to load with error: " + error.GetMessage());
            OnAdFailedToLoadEvent.Invoke();
        };
        _bannerView.OnAdImpressionRecorded += () =>
        {
            PrintStatus("Banner ad recorded an impression.");
        };
        _bannerView.OnAdClicked += () =>
        {
            PrintStatus("Banner ad recorded a click.");
        };
        _bannerView.OnAdFullScreenContentOpened += () =>
        {
            PrintStatus("Banner ad opening.");
            OnAdOpeningEvent.Invoke();
        };
        _bannerView.OnAdFullScreenContentClosed += () =>
        {
            PrintStatus("Banner ad closed.");
            OnAdClosedEvent.Invoke();
        };
        _bannerView.OnAdPaid += (AdValue adValue) =>
        {
            string msg = string.Format("{0} (currency: {1}, value: {2}",
                                        "Banner ad received a paid event.",
                                        adValue.CurrencyCode,
                                        adValue.Value);
            PrintStatus(msg);
        };
        // Load a banner ad
        _bannerView.LoadAd(CreateAdRequest());
    }
private void RequestInterstitial()
{
    #if UNITY_EDITOR
    string adUnitId = "unused";
    #elif UNITY_ANDROID
    string adUnitId = "ca-app-pub-xxxx";
    if (_isDebugBuild)
        adUnitId = "ca-app-pub-3940256099942544/1033173712"; //Fake
    #elif (UNITY_5 && UNITY_IOS) || UNITY_IPHONE
    string adUnitId = "ca-app-pub-xxxx";
    #else
    string adUnitId = "unexpected_platform";
    #endif

    // Clean up interstitial before using it
    if (_interstitialAd != null)
    {
        _interstitialAd.Destroy();
    }

    // Load an interstitial ad
    InterstitialAd.Load(adUnitId, CreateAdRequest(),
        (InterstitialAd ad, LoadAdError loadError) =>
        {
            if (loadError != null)
            {
                PrintStatus("Interstitial ad failed to load with error: " + loadError.GetMessage());
                return;
            }
            else if (ad == null)
            {
                PrintStatus("Interstitial ad failed to load.");
                return;
            }

            PrintStatus("Interstitial ad loaded.");
            _interstitialAd = ad;

            ad.OnAdFullScreenContentOpened += () =>
            {
                PrintStatus("Interstitial ad opening.");
                OnAdOpeningEvent.Invoke();
            };
            ad.OnAdFullScreenContentClosed += () =>
            {
                PrintStatus("Interstitial ad closed.");
                OnAdClosedEvent.Invoke();
            };
            ad.OnAdImpressionRecorded += () =>
            {
                PrintStatus("Interstitial ad recorded an impression.");
            };
            ad.OnAdClicked += () =>
            {
                PrintStatus("Interstitial ad recorded a click.");
            };
            ad.OnAdFullScreenContentFailed += (AdError error) =>
            {
                PrintStatus("Interstitial ad failed to show with error: " + error.GetMessage());
            };
            ad.OnAdPaid += (AdValue adValue) =>
            {
                string msg = string.Format("{0} (currency: {1}, value: {2}",
                                           "Interstitial ad received a paid event.",
                                           adValue.CurrencyCode,
                                           adValue.Value);
                PrintStatus(msg);
            };
        });
}

Originally posted by @eirikost in https://github.com/googleads/googleads-mobile-unity/issues/2937#issuecomment-1728494363