本文介绍了Unity-Admob隐藏横幅不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我进入名为主要的下一个场景时,为什么我的admob横幅没有隐藏?
我做了其他人在其他线程上所说的一切。.

Why doesn't my admob banner hide when I go the next scene called ''Main''?I did everything what other people said on other threads..

这是我的代码:

using GoogleMobileAds.Api;

public class AdmobAds : MonoBehaviour {

private BannerView bannerView;


    private void RequestBanner()
    {
        #if UNITY_ANDROID
        string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxx";
        #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 bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the banner with the request.
        bannerView.LoadAd(request);
    }

    public void HideAd()
    {
        bannerView.Destroy ();
        bannerView.Hide ();
    }

    void Start()
    {
        Scene currentScene = SceneManager.GetActiveScene ();
        string sceneName = currentScene.name;

        if (sceneName == "Menu")
        {
            RequestBanner ();
        }

        else if (sceneName == "Main")
        {
            bannerView.Destroy ();
            bannerView.Hide ();
        }
    }
}

也HideAd已附加到开始按钮上,但它仍然没有隐藏横幅。

Also the ''public void HideAd'' is attachted to the start button, still it doesn't hide the banner..

我该怎么办?

推荐答案

问题出在 RequestBanner 函数中:

BannerView bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);

bannerView 是局部变量,新的 BannerView 实例将存储到该本地 bannerView 变量,而不是全局 bannerView 变量。

The bannerView is a local variable and the new BannerView instance will be stored to that local bannerView variable instead of the global bannerView variable.

您需要将 BannerView 实例存储在全局<$ c $中c> bannerView 变量。

You need that BannerView instance to be stored in the global bannerView variable.

应更改为:

bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);

另一个问题在这里:

public void HideAd()
{
    bannerView.Destroy ();
    bannerView.Hide ();
}

您正在销毁 bannerView 在隐藏它之前。情况应该相反。您应该隐藏,然后销毁 bannerView 。如果确实如此,只需隐藏 bannerView 即可。您不必销毁它。

You are destroying bannerView before hiding it. It should be the other way around. You should Hide then Destroy the bannerView. If fact, simply Hiding the bannerView should be fine. You don't have to Destroy it.

这篇关于Unity-Admob隐藏横幅不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 07:39