0

i am new to unity. I made an application and decide to add advertisement to it. user can delete the advertisement when they clicked a button. so i put the googlemobileadsdemoscript to a gameobject and the button would destroy the gameobject when user click it. the problem is.. its not working. the gameobject was deleted but the advertisement is still there. can you guys help me.. heres the code. i renamed it to 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

}

and the button script

 public void RemoveAd()
    {

        Destroy (GameObject.Find ("Advertistment"));
    Application.LoadLevel ("minigame");//this would replay the game
    }
}
Adrian Krupa
  • 1,877
  • 1
  • 15
  • 24
chen
  • 1
  • 1

1 Answers1

1

Maybe you should use this script instead of deleting it.

Try calling:

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

Of course it would be good to add some NULL checking.

Adrian Krupa
  • 1,877
  • 1
  • 15
  • 24
  • thank you for your suggestion. i have already solved it. i actually have to change the ad unit id string adUnitId = "ca-app-pub-3110192020641644/9651420212"; to anything else so the advertisement can stop. – chen Oct 16 '15 at 08:09