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

问题描述

当我进入下一个名为Main"的场景时,为什么我的 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 ();
        }
    }
}

还有public void 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 实例存储在全局 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:38