在代码中将“请求非页内广告”功能放到哪里?

时间:2019-07-03 23:43:37

标签: unity3d admob

无论何时我想向他们展示广告,您都认为投放此功能的正确位置是什么?是Start()吗?生成游戏对象后,就会调用Start,这样继续拥有广告的情况就会变得很糟糕

public void RequestInterstitialAd()
{
    interstitalAd = new InterstitialAd(interstitalAdID);
    AdRequest adRequest = new AdRequest.Builder().Build();
    interstitalAd.LoadAd(adRequest);

    interstitalAd.OnAdLoaded += HandleOnAdLoaded;
    interstitalAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
    interstitalAd.OnAdOpening += HandleOnAdOpened;
    interstitalAd.OnAdClosed += HandleOnAdClosed;
    interstitalAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
}

2 个答案:

答案 0 :(得分:0)

是的,在静态GameObject的“开始”方法中进行操作。因为它需要初始化一次,而不是多次。

答案 1 :(得分:0)

我每次需要展示广告时都需要调用它,所以我这样做了:

public class AdMobManager : MonoBehaviour
{
    public static AdMobManager instance;//to have the right to call this class functions and variables without the need to put static to each one from another class 
    [SerializeField]
    private string appID,interstitalAdID;
    private InterstitialAd interstitalAd;
    public bool isAdOpened,isAdClosed;
    // Start is called before the first frame update
    void Start()
    {
        instance = this;
        MobileAds.Initialize(appID);
        RequestInterstitialAd();//load an ad from the first time game is opened
    }

    // Update is called once per frame
    void Update()
    {
    }

    public void RequestInterstitialAd()
    {
        if(interstitalAd != null)//remove the old interstital ad showed before to create a new one 
        {
            interstitalAd.Destroy();
        }
        interstitalAd = new InterstitialAd(interstitalAdID);
        AdRequest adRequest = new AdRequest.Builder().Build();
        interstitalAd.LoadAd(adRequest);
        interstitalAd.OnAdLoaded += HandleOnAdLoaded;
        interstitalAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
        interstitalAd.OnAdOpening += HandleOnAdOpened;
        interstitalAd.OnAdClosed += HandleOnAdClosed;
        interstitalAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
        Debug.Log("Ads Request Created");//used to test
    }

    public void DisplayInterstitialAd()
    {
        if (interstitalAd.IsLoaded())
        {
            interstitalAd.Show();
        }
    }

    public void HandleOnAdLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLoaded event received");
    }
    public void HandleOnAdFailedToLoad(object sender,AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print("HandleFailedToReciveAd event received with message: " + args.Message);
    }
    public void HandleOnAdOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdOpened event received");
        isAdOpened = true;
    }
    public void HandleOnAdClosed(object sender,EventArgs args)
    {
        MonoBehaviour.print("HandleAdClosed event received");
        isAdClosed = true;
        isAdOpened = false;//initialise
        RequestInterstitialAd();//everytime we show an ad load another one when the first one is closed
    }
    public void HandleOnAdLeavingApplication(object sender,EventArgs args)
    {
        MonoBehaviour.print("HandleAdLeavingApplication event recived");
    }


}