TransWikia.com

Не могу настроить рекламу admob в unity приложении

Stack Overflow на русском Asked by Madoka Magica on January 14, 2021

Приложение уже сбилдил с ключем, все id вроде правильные тут брал на картинке. Запускаю на смартфоне постоянно выбрасывает ошибку:HandleFailedToReceiveAd event received with message: Nofill. Я прочитал что это за ошибка, что то типа нет подходящих размеров рекламы. Ладно, пусть, но я менял индефикаторы на индефикаторы рекламы моего работающего в гугл плей приложения (ну правда это игра, но не суть) и получал ту же самую ошибку. Как такое возможно, ведь если запускать работающее приложение реклама там работает и показывается? Может так и должно быть? Просто вобще не могу увидеть проклятый баннер, может его что закрывает
Вот стандартный код подключения, я его немного переделал :

using UnityEngine.Events;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
using System.Collections;
//это мое
public class GoogleAdMobController : MonoBehaviour
{
    public static GoogleAdMobController instance;
    private BannerView bannerView;
    private InterstitialAd interstitialAd;
    private RewardedAd rewardedAd;
    private float deltaTime;

    public UnityEvent OnAdLoadedEvent;
    public UnityEvent OnAdFailedToLoadEvent;
    public UnityEvent OnAdOpeningEvent;
    public UnityEvent OnAdFailedToShowEvent;
    public UnityEvent OnUserEarnedRewardEvent;
    public UnityEvent OnAdClosedEvent;
    public UnityEvent OnAdLeavingApplicationEvent;
    public bool showFpsMeter = true;
    //public Text fpsMeter;
    public Text statusText;

    #region UNITY MONOBEHAVIOR METHODS

    private void OnEnable()
    {
        instance = this;
    }
    public void Start()
    {
        MobileAds.SetiOSAppPauseOnBackground(true);

        List<String> deviceIds = new List<String>() { AdRequest.TestDeviceSimulator };

        // Add some test device IDs (replace with your own device IDs).
#if UNITY_IPHONE
        deviceIds.Add("96e23e80653bb28980d3f40beb58915c");
#elif UNITY_ANDROID
        deviceIds.Add("3A9SDBC4160dfdgA6C");
#endif

        // Configure TagForChildDirectedTreatment and test device IDs.
        RequestConfiguration requestConfiguration =
            new RequestConfiguration.Builder()
            .SetTagForChildDirectedTreatment(TagForChildDirectedTreatment.Unspecified)
            //.SetTestDeviceIds(deviceIds)
            .build();

        MobileAds.SetRequestConfiguration(requestConfiguration);

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(HandleInitCompleteAction);
    }

    private void HandleInitCompleteAction(InitializationStatus initstatus)
    {
        // Callbacks from GoogleMobileAds are not guaranteed to be called on
        // main thread.
        // In this example we use MobileAdsEventExecutor to schedule these calls on
        // the next Update() loop.
        MobileAdsEventExecutor.ExecuteInUpdate(() => {
            UiManager.instance.tstText.text = "Initialization complete";
           // RequestAndLoadInterstitialAd();
             RequestBannerAd();
            /* RequestAndLoadInterstitialAd();*/
        });
    }

   

    #endregion

    #region HELPER METHODS

    private AdRequest CreateAdRequest()
    {
        return new AdRequest.Builder()
           /* .AddTestDevice(AdRequest.TestDeviceSimulator)
            .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
            .AddKeyword("unity-admob-sample")
            .TagForChildDirectedTreatment(false)
            .AddExtra("color_bg", "9B30FF")*/
            .Build();
    }

    #endregion

    #region BANNER ADS

    public void RequestBannerAd()
    {
        UiManager.instance.tstText.text = "Requesting Banner Ad.";
        // These ad units are configured to always serve test ads.
#if UNITY_EDITOR
        string adUnitId = "none";
#elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-5467474674676746/3765755656";
#elif UNITY_IPHONE
        string adUnitId = "";
#else
        string adUnitId = "unexpected_platform";
#endif
        // Clean up banner before reusing
        if (bannerView != null)
        {
            bannerView.Destroy();
        }

        // Create a 320x50 banner at top of the screen
        bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);

        // Add Event Handlers
        /* bannerView.OnAdLoaded += (sender, args) => OnAdLoadedEvent.Invoke();
         //  bannerView.OnAdFailedToLoad += (sender, args) => OnAdFailedToLoadEvent.Invoke();
         this.bannerView.OnAdFailedToLoad += this.HandleOnAdFailedToLoad;
         bannerView.OnAdOpening += (sender, args) => OnAdOpeningEvent.Invoke();
         bannerView.OnAdClosed += (sender, args) => OnAdClosedEvent.Invoke();
         bannerView.OnAdLeavingApplication += (sender, args) => OnAdLeavingApplicationEvent.Invoke();*/

        // Called when an ad request has successfully loaded.
        this.bannerView.OnAdLoaded += this.HandleOnAdLoaded;
        // Called when an ad request failed to load.
        this.bannerView.OnAdFailedToLoad += this.HandleOnAdFailedToLoad;
        // Called when an ad is clicked.
        this.bannerView.OnAdOpening += this.HandleOnAdOpened;
        // Called when the user returned from the app after an ad click.
        this.bannerView.OnAdClosed += this.HandleOnAdClosed;
        // Called when the ad click caused the user to leave the application.
        this.bannerView.OnAdLeavingApplication += this.HandleOnAdLeavingApplication;

        // Load a banner ad
        bannerView.LoadAd(CreateAdRequest());
        
        bannerView.Show();

    }

   

    public void DestroyBannerAd()
    {
        if (bannerView != null)
        {
            bannerView.Destroy();
        }
    }

    #endregion

    #region INTERSTITIAL ADS

    public void RequestAndLoadInterstitialAd()
    {
        UiManager.instance.tstText.text = "Requesting Interstitial Ad.";
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-58585857568585676767/5464677";
#elif UNITY_IPHONE
        string adUnitId = "";
#else
        string adUnitId = "unexpected_platform";
#endif

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

        interstitialAd = new InterstitialAd(adUnitId);

        // Add Event Handlers
        // Register for ad events.
        this.interstitialAd.OnAdLoaded += this.HandleInterstitialLoaded;
        this.interstitialAd.OnAdFailedToLoad += this.HandleInterstitialFailedToLoad;
        this.interstitialAd.OnAdOpening += this.HandleInterstitialOpened;
        this.interstitialAd.OnAdClosed += this.HandleInterstitialClosed;
        this.interstitialAd.OnAdLeavingApplication += this.HandleInterstitialLeftApplication;

        // Load an interstitial ad
        interstitialAd.LoadAd(CreateAdRequest());
    }

    public void ShowInterstitialAd()
    {
        if (interstitialAd.IsLoaded())
        {
            interstitialAd.Show();
        }
        else
        {
            UiManager.instance.tstText.text = "Interstitial ad is not ready yet";
        }
    }

    public void DestroyInterstitialAd()
    {
        if (interstitialAd != null)
        {
            interstitialAd.Destroy();
        }
    }
    #endregion

    #region REWARDED ADS

    public void RequestAndLoadRewardedAd()
    {
        UiManager.instance.tstText.text = "Requesting Rewarded Ad.";
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-58585857568585676767/546656";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
        string adUnitId = "unexpected_platform";
#endif

        // create new rewarded ad instance
        rewardedAd = new RewardedAd(adUnitId);
        // Called when an ad request has successfully loaded.
        this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
        // Called when an ad request failed to load.
        this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
        // Called when an ad is shown.
        this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
        // Called when an ad request failed to show.
        this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
        // Called when the user should be rewarded for interacting with the ad.
        this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
        // Called when the ad is closed.
        this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;
        // Create empty ad request
        rewardedAd.LoadAd(CreateAdRequest());
    }

    public void ShowRewardedAd()
    {
        if (rewardedAd != null)
        {
            rewardedAd.Show();
        }
        else
        {
            UiManager.instance.tstText.text = "Rewarded ad is not ready yet.";
        }
    }

    #endregion

    #region
    public void HandleOnAdLoaded(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text = "HandleAdLoaded event received";
    }

    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        UiManager.instance.tstText.text = "HandleFailedToReceiveAd event received with message: "
                            + args.Message;
        StartCoroutine(corAgainStartBanner());
    }

    IEnumerator corAgainStartBanner() {
        yield return new WaitForSecondsRealtime(30f);
        RequestBannerAd();
    }

    public void HandleOnAdOpened(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text = "HandleAdOpened event received";
    }

    public void HandleOnAdClosed(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text = "HandleAdClosed event received";
    }

    public void HandleOnAdLeavingApplication(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text = "HandleAdLeavingApplication event received";
    }
    #endregion

    #region Intrsteliar
    public void HandleInterstitialLoaded(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text = "HandleInterstitialLoaded event received";
        //ShowInterstitial();
    }

    public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        UiManager.instance.tstText.text =
            "HandleInterstitialFailedToLoad event received with message: " + args.Message;
    }

    public void HandleInterstitialOpened(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text ="HandleInterstitialOpened event received";
    }

    public void HandleInterstitialClosed(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text = "HandleInterstitialClosed event received";
    }

    public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text = "HandleInterstitialLeftApplication event received";
    }
    #endregion

    #region Rewarded
    public void HandleRewardedAdLoaded(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text = "HandleRewardedAdLoaded event received";
    }

    public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args)
    {
        UiManager.instance.tstText.text =
            "HandleRewardedAdFailedToLoad event received with message: "
                             + args.Message;
    }

    public void HandleRewardedAdOpening(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text = "HandleRewardedAdOpening event received";
    }

    public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
    {
        UiManager.instance.tstText.text =
            "HandleRewardedAdFailedToShow event received with message: "
                             + args.Message;
    }

    public void HandleRewardedAdClosed(object sender, EventArgs args)
    {
        UiManager.instance.tstText.text = "HandleRewardedAdClosed event received";
    }

    public void HandleUserEarnedReward(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;
        UiManager.instance.tstText.text =
            "HandleRewardedAdRewarded event received for "
                        + amount.ToString() + " " + type;
    }
    #endregion
}

введите сюда описание изображения
введите сюда описание изображения

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP