级别重启后的Unity android横幅广告问题

时间:2015-09-15 19:49:04

标签: android unity3d admob banner

在我的游戏中当角色死亡时我想要显示横幅并在玩家按下重启按钮时隐藏它。它的工作正常但在第一次重置后我想显示横幅我得到错误

  

NullReferenceException:未将对象引用设置为对象的实例   GoogleMobileAdsDemoScript.showBanner()

所以我应该在每次游戏重启后请求横幅吗?它不会太迟钝?

我还尝试将DontDestroyOnLoad (this);附加到包含我的广告脚本的对象,但它没有用。

我的重启按钮:Application.LoadLevel (Application.loadedLevel);

AD脚本`

private BannerView bannerView;
private InterstitialAd interstitial;
private static string outputMessage = "";

public static string OutputMessage
{
    set { outputMessage = value; }
}


private void RequestBanner()
{
    #if UNITY_EDITOR
        string adUnitId = "unused";
    #elif UNITY_ANDROID
    string adUnitId = "myai";
    #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());
}

private void RequestInterstitial()
{
    #if UNITY_EDITOR
        string adUnitId = "unused";
    #elif UNITY_ANDROID
        string adUnitId = "INSERT_ANDROID_INTERSTITIAL_AD_UNIT_ID_HERE";
    #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)
{
    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

` 我该怎么办?

1 个答案:

答案 0 :(得分:0)

我建议您使用Appodeal插件。