删除游戏对象删除admob。不工作

时间:2015-10-15 04:48:12

标签: c# unity3d admob destroy gameobject

我是团结的新手。我做了一个申请并决定添加广告。用户可以在点击按钮时删除广告。所以我将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
    }
}

1 个答案:

答案 0 :(得分:1)

也许您应该使用此脚本而不是删除它。

尝试致电:

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

当然最好添加一些NULL检查。