2015-10-15 57 views
0

我是新來的團結。我做了一個應用程序,並決定添加廣告。用戶可以在點擊按鈕時刪除廣告。所以我把googlemobileadsdemoscript放到一個gameobject中,當用戶點擊它時,這個按鈕會摧毀gameobject。問題是......它不工作。 gameobject被刪除,但廣告仍然存在。你們可以幫我嗎.. 繼承人的代碼。我把它改名爲DisableAd.cs通過刪除gameobject來刪除admob。不工作

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("ABCDEFABCDEF") 
       .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 
    } 
} 

回答

1

也許你應該使用刪除它的這個腳本。

嘗試調用:

GameObject.Find ("Advertistment").GetComponent<DisableAd>().RemoveAds(); 

當然,這將是很好的補充一些NULL檢查。

+0

感謝您的建議。我已經解決了它。我實際上必須更改廣告單元ID字符串adUnitId =「ca-app-pub-3110192020641644/9651420212」;到其他任何東西,廣告可以停止。 – chen