这是我第一次实施广告。我正在使用admob。我一直在弄乱不同的代码作品,但是如果我销毁了它,我似乎无法创建一个新广告。
private void RequestInterstitial()
{
// These ad units are configured to always serve test ads.
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-3895731736666139/7005438200";
#elif UNITY_IPHONE
string adUnitId = "hmm";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
this.interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
this.interstitial.OnAdLoaded += this.HandleInterstitialLoaded;
this.interstitial.OnAdFailedToLoad += this.HandleInterstitialFailedToLoad;
this.interstitial.OnAdOpening += this.HandleInterstitialOpened;
this.interstitial.OnAdClosed += this.HandleInterstitialClosed;
this.interstitial.OnAdLeavingApplication += this.HandleInterstitialLeftApplication;
// Load an interstitial ad.
this.interstitial.LoadAd(this.CreateAdRequest());
}
private void ShowInterstitial()
{
if (this.interstitial.IsLoaded())
{
this.interstitial.Show();
}
else
{
MonoBehaviour.print("Interstitial is not ready yet");
text.text = "No Ad Loaded";
}
}
现在销毁
this.interstitial.Destroy();
单击间隙上的" x"时,销毁功能会被调用吗?
那我该如何带回?
单击" x"时,销毁功能会被调用吗 间隙?
否。不太可能。如果这是真的,那么您必须每次执行new InterstitialAd(adUnitId);
,然后再再次显示AD。单击x时,很可能将InterstitialAd.Hide();
称为。
我一直在弄乱不同的代码件,但我不能 如果我已经销毁了一个新广告,似乎会创建一个新广告。
那我该如何带回?
,除非间隙声明不超出范围,否则您绝对不应销毁它,然后您应该致电interstitial.Destroy()
。
例如:
如果在函数中声明间隙,则必须在该功能末尾销毁它以避免内存泄漏。
如果在函数/全局变量之外声明间隙,则只能在Unity OnDisable()
函数中销毁它。就您而言,它是一个全局变量,因此必须将interstitial.Destroy()
放入OnDisable()
函数中,以便当您的脚本破坏时,它也会破坏interstitial
。
现在,如果您最终破坏了间隙,则可以再次进行interstitial = new InterstitialAd(adUnitId);
,然后致电interstitial.LoadAd()
,然后拨打interstitial.Show()
。在调用interstitial.Show()
之前,您必须首先检查它是否已使用if(interstitial.IsLoaded())
完成加载,然后再致电interstitial.Show()
。
最后,当您注册事件时,您应该在OnDisable()
函数中未注册该事件。您在+=
注册,并用-=
注册。因此,您应该在调用interstitial.Destroy()
之前使用this.interstitial.OnAdLoaded -= this.HandleInterstitialLoaded;
和OnDisable()
功能中的其他事件来执行此操作。