AdManager给出错误,但脚本中未显示任何错误

时间:2019-05-05 10:55:44

标签: unity3d admob game-development

广告管理器脚本没有显示任何错误,但是当我在“显示广告按钮”中将脚本添加到Click()时,脚本没有显示任何功能。 在控制台中,我收到此错误:

  

NullReferenceException:对象引用未设置为对象的实例   AdManager.HandleBannerADEvents(System.Boolean订阅)(位于Assets / Scripts / AdManager.cs:151)   AdManager.OnEnable()(位于Assets / Scripts / AdManager.cs:177)

这是我的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System;






public class AdManager : MonoBehaviour
{
    private string APP_ID = "ca-app-pub-5414609211143331~8737814075";
    private BannerView bannerAD;
    private InterstitialAd interstitialAd;
    private RewardBasedVideoAd rewardVideoAd;

    void Start()
    {
        // MobileAds.Initialize(APP_ID);
        RequestBanner();
        RequestInterstitial();
        RequestVideoAD();




    }

    void RequestBanner()
    {
        string banner_ID = "ca-app-pub-3940256099942544/6300978111";
        bannerAD = new BannerView(banner_ID, AdSize.SmartBanner, AdPosition.Top);

        //for real app
        //AdRequest adRequest = new AdRequest.Builder().Build();





        //for test
        AdRequest adRequest = new AdRequest.Builder().AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();

        bannerAD.LoadAd(adRequest);

    }
    void RequestInterstitial()
    {
        string interstitial_ID = "ca-app-pub-3940256099942544/1033173712";
        interstitialAd = new InterstitialAd(interstitial_ID);



        //for real app
        //AdRequest adRequest = new AdRequest.Builder().Build();





        //for test
        AdRequest adRequest = new AdRequest.Builder().AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();

        interstitialAd.LoadAd(adRequest);

    }
    void RequestVideoAD()
    {
        string video_ID = "ca-app-pub-3940256099942544/5224354917";
        rewardVideoAd = RewardBasedVideoAd.Instance;



        //for real app
        //AdRequest adRequest = new AdRequest.Builder().Build();





        //for test
        AdRequest adRequest = new AdRequest.Builder().AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();

        rewardVideoAd.LoadAd(adRequest, video_ID);

    }


    public void Display_Banner()
    {
        bannerAD.Show();

    }
    public void Display_IntersitialAD()
    {
        if (interstitialAd.IsLoaded())
        {
            interstitialAd.Show();


        }
    }

    public void Display_Reward_Video()
    {
        if (rewardVideoAd.IsLoaded())
        {
            rewardVideoAd.Show();

        }




    }


    //HandleEvent
    public void HandleOnAdLoaded(object sender, EventArgs args)
    {
        Display_Banner();
    }

    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        RequestBanner();

    }

    public void HandleOnAdOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdOpened event received");
    }

    public void HandleOnAdClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdClosed event received");
    }

    public void HandleOnAdLeavingApplication(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLeavingApplication event received");
    }
    void HandleBannerADEvents(bool subscribe)
    {
        if(subscribe)
        {
            // Called when an ad request has successfully loaded.
            bannerAD.OnAdLoaded += HandleOnAdLoaded;
            // Called when an ad request failed to load.
            bannerAD.OnAdFailedToLoad += HandleOnAdFailedToLoad;
            // Called when an ad is clicked.
            bannerAD.OnAdOpening += HandleOnAdOpened;
            // Called when the user returned from the app after an ad click.
            bannerAD.OnAdClosed += HandleOnAdClosed;
            // Called when the ad click caused the user to leave the application.
            bannerAD.OnAdLeavingApplication += HandleOnAdLeavingApplication;
        } 
        else
        {
            // Called when an ad request has successfully loaded.
            bannerAD.OnAdLoaded -= HandleOnAdLoaded;
            // Called when an ad request failed to load.
            bannerAD.OnAdFailedToLoad += HandleOnAdFailedToLoad;
            // Called when an ad is clicked.
            bannerAD.OnAdOpening -= HandleOnAdOpened;
            // Called when the user returned from the app after an ad click.
            bannerAD.OnAdClosed -= HandleOnAdClosed;
            // Called when the ad click caused the user to leave the application.
            bannerAD.OnAdLeavingApplication -= HandleOnAdLeavingApplication;
        }
    }
     void OnEnable()
    {
        HandleBannerADEvents(true);
    }
     void OnDisable()
    {
        HandleBannerADEvents(false);
    }
}

0 个答案:

没有答案