通过删除gameobject删除admob.不工作
本文关键字:删除 工作 admob gameobject | 更新日期: 2023-09-27 18:17:57
我是unity的新手。我做了一个申请,并决定在其中添加广告。用户点击一个按钮就可以删除广告。所以我将googlemobileadsdemoscript放到一个游戏对象中,当用户点击它时,按钮将破坏游戏对象。问题是……不管用。游戏对象被删除了,但广告还在。你们能帮我…下面是代码。我把它重命名为DisableAd.cs
using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class DisableAd : MonoBehaviour {
private BannerView bannerView;
private InterstitialAd interstitial;
private static string outputMessage = "";
void Awake()
{
RequestBanner();
}
public static string OutputMessage
{
set { outputMessage = value; }
}
void RequestBanner()
{
if(PlayerPrefs.HasKey("AdFree"))
return;
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-3110192020641644/9651420212";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
// Register for ad events.
bannerView.AdLoaded += HandleAdLoaded;
bannerView.AdFailedToLoad += HandleAdFailedToLoad;
bannerView.AdOpened += HandleAdOpened;
bannerView.AdClosing += HandleAdClosing;
bannerView.AdClosed += HandleAdClosed;
bannerView.AdLeftApplication += HandleAdLeftApplication;
// Load a banner ad.
bannerView.LoadAd(createAdRequest());
}
void RemoveAds()
{
if (PlayerPrefs.HasKey("AdFree"))
print("Ads already removed");
else{
PlayerPrefs.SetInt("AdFree", 1);
PlayerPrefs.Save();
// destroy/disable all your ad objects here
}
}
//void RestorePurchases()
//{
// if (IsProductPurchased("ProductId"))
// RemoveAds();
//}
private void RequestInterstitial()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-3110192020641644/2128153413";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
interstitial.AdLoaded += HandleInterstitialLoaded;
interstitial.AdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.AdOpened += HandleInterstitialOpened;
interstitial.AdClosing += HandleInterstitialClosing;
interstitial.AdClosed += HandleInterstitialClosed;
interstitial.AdLeftApplication += HandleInterstitialLeftApplication;
GoogleMobileAdsDemoHandler handler = new GoogleMobileAdsDemoHandler();
interstitial.SetInAppPurchaseHandler(handler);
// Load an interstitial ad.
interstitial.LoadAd(createAdRequest());
}
// Returns an ad request with custom ad targeting.
private AdRequest createAdRequest()
{
return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
.AddKeyword("game")
.SetGender(Gender.Male)
.SetBirthday(new DateTime(1985, 1, 1))
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();
}
private void ShowInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
print("Interstitial is not ready yet.");
}
}
#region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args)
{
bannerView.Show ();
print("HandleAdLoaded event received.");
}
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleFailedToReceiveAd event received with message: " + args.Message);
}
public void HandleAdOpened(object sender, EventArgs args)
{
print("HandleAdOpened event received");
}
void HandleAdClosing(object sender, EventArgs args)
{
print("HandleAdClosing event received");
}
public void HandleAdClosed(object sender, EventArgs args)
{
print("HandleAdClosed event received");
}
public void HandleAdLeftApplication(object sender, EventArgs args)
{
print("HandleAdLeftApplication event received");
}
#endregion
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
print("HandleInterstitialLoaded event received.");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
print("HandleInterstitialOpened event received");
}
void HandleInterstitialClosing(object sender, EventArgs args)
{
print("HandleInterstitialClosing event received");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
print("HandleInterstitialClosed event received");
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
print("HandleInterstitialLeftApplication event received");
}
#endregion
}
和按钮脚本
public void RemoveAd()
{
Destroy (GameObject.Find ("Advertistment"));
Application.LoadLevel ("minigame");//this would replay the game
}
}
也许你应该使用这个脚本,而不是删除它。
试着调用:
GameObject.Find ("Advertistment").GetComponent<DisableAd>().RemoveAds();
当然,添加一些NULL
检查会很好。