Unity-Google Admob测试广告有时只会显示

时间:2019-03-23 19:56:39

标签: c# unity3d admob

由于某些原因,有时只会显示测试广告,横幅总是显示。它们还会显示在控制台中,但是当我在设备上构建时,它们不会显示。然后,在UI上的按钮后面进行设置。任何帮助表示感谢,谢谢!如果您需要更多信息,请告诉我。我不确定为什么广告有时只显示,而大多数时候却不显示。

     // Banner View
    private BannerView bannerView;
    // InterstitialAd
    private  InterstitialAd interstitial;

    private RewardBasedVideoAd rewardBasedVideo;


    public void Start()
        {
    #if UNITY_ANDROID
                string appId = "****";
#elif UNITY_IPHONE
                string appId = "*****";
#else
            string appId = "unexpected_platform";
#endif

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

            this.RequestBanner();

        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;
        this.RequestRewardBasedVideo();
        this.RequestInterstitial();


        //google video callbacks
        // Called when an ad request has successfully loaded.
        rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
        // Called when an ad request failed to load.
        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
        // Called when an ad is shown.
        rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
        // Called when the ad starts to play.
        rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
        // Called when the user should be rewarded for watching a video.
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        // Called when the ad is closed.
        rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
        // Called when the ad click caused the user to leave the application.
        rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;

    }




    //Requesting banner.
    private void RequestBanner()
        {
    #if UNITY_ANDROID
                string adUnitId = "ca-app-pub-3940256099942544/6300978111";
    #elif UNITY_IPHONE
                string adUnitId = "ca-app-pub-3940256099942544/6300978111";
    #else
            string adUnitId = "unexpected_platform";
    #endif

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

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


        // Create an empty ad request.  add a test device.
            AdRequest request = new AdRequest.Builder().AddTestDevice(AdRequest.TestDeviceSimulator).AddTestDevice(SystemInfo.deviceUniqueIdentifier).Build();

            // Load the banner with the request.
            bannerView.LoadAd(request);

            bannerView.Destroy();

        }

    public void showInterstitialAd()
    {
        if (this.interstitial != null)
        {
            if (this.interstitial.IsLoaded())
                this.interstitial.Show();
            Debug.Log("Showing interstitial advert!");
        }
        else
        {
            Debug.Log("The interstitial has not loaded sucessfully.");
        }
    }

    //Requesting interstitial ads.
    private void RequestInterstitial()
        {
#if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#else
        string adUnitId = "unexpected_platform";
    #endif

            // Initialize an InterstitialAd.
            this.interstitial = new InterstitialAd(adUnitId);

            // Called when an ad request has successfully loaded.
            this.interstitial.OnAdLoaded += HandleOnAdLoaded1;
            // Called when an ad request failed to load.
            this.interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad1;
            // Called when an ad is shown.
            this.interstitial.OnAdOpening += HandleOnAdOpened1;
            // Called when the ad is closed.
            this.interstitial.OnAdClosed += HandleOnAdClosed1;
            // Called when the ad click caused the user to leave the application.
            this.interstitial.OnAdLeavingApplication += HandleOnAdLeavingApplication1;

        // Create an empty ad request.  add a test device.
            AdRequest request = new AdRequest.Builder().AddTestDevice(AdRequest.TestDeviceSimulator).AddTestDevice(SystemInfo.deviceUniqueIdentifier).Build();
        // Load the interstitial with the request.
        this.interstitial.LoadAd(request);

        }


    private void RequestRewardBasedVideo()
    {
#if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#else
        string adUnitId = "unexpected_platform";
#endif

        // Create an empty ad request. add a test device.
        AdRequest request = new AdRequest.Builder().AddTestDevice(AdRequest.TestDeviceSimulator).AddTestDevice(SystemInfo.deviceUniqueIdentifier).Build();
        // Load the rewarded video ad with the request.
        this.rewardBasedVideo.LoadAd(request, adUnitId);
    }


    public void showGoogleVideo()
    {
        if (this.rewardBasedVideo != null)
            if (this.rewardBasedVideo.IsLoaded())
            {
                Debug.Log("Video is showing");
                this.rewardBasedVideo.Show();

            }
            else
            {
                Debug.Log("The video ad has not sucessfully loaded");
            }
    }

0 个答案:

没有答案