Unity3d 销毁广告(adMob)后如何将其带回?
这是我第一次实施广告。我正在使用adMob。我一直在乱搞不同的代码块,但我似乎无法创建一个新的广告,如果我破坏了它Unity3d 销毁广告(adMob)后如何将其带回?,unity3d,admob,Unity3d,Admob,这是我第一次实施广告。我正在使用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
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”时,是否调用销毁函数
间隙的
否。不太可能。如果这是真的,那么您必须执行newinterstituralad(adUnitId)再次显示广告前,每次代码>。单击x时,很可能是interstitalad.Hide()代码>被调用
我一直在处理不同的代码片段,但我不能
如果我把它毁了的话,它似乎在创造一个新的广告
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";
}
}
那我怎么把它带回来呢
除非填隙声明超出范围,否则不应销毁它,然后应调用interstival.destroy()
例如:
如果在函数中声明了interstival,则必须在该函数结束时销毁它,以避免内存泄漏
如果在函数/全局变量外部声明Interstival,则只应在UnityOnDisable()
函数中销毁它。在您的情况下,它是一个全局变量,因此必须将interstitual.Destroy()
放在OnDisable()
函数中,这样当脚本销毁时,它也将销毁interstitual
现在,如果你最终破坏了间质,你可以通过做interstival=newinterstitalad(adUnitId)把它带回来再次调用interstival.LoadAd()
,然后调用interstival.Show()
。在调用interstitual.Show()
之前,必须先检查它是否已使用if(interstitual.IsLoaded())
完成加载,然后再调用interstitual.Show()
最后,当您注册一个事件时,您还应该在OnDisable()
函数中取消注册该事件。您可以使用+=
注册,也可以使用-=
取消注册。因此,您应该使用this.interstitual.OnAdLoaded-=this.handleinterstituialloaded调用interstitial.Destroy()
函数之前,OnDisable()
函数中的code>和其他事件也是如此。谢谢您的帮助!不客气。你可以从我的网站上得到我刚才提到的示例代码