首页 > 解决方案 > Unity 当按钮单击时显示 admob 插页式

问题描述

我想添加 admob 插页式广告,当按钮单击显示广告时,广告关闭后“更改车辆”菜单这是我的“更改车辆”按钮代码

            if( GUI.Button(new Rect(Screen.width - 270, 330, 240, 50), "Change Vehicle") )
        {
            selectScreen = true;
            objects[activeObjectIdx].GetComponent<CarControllerV2>().canControl = false;
            GetComponent<CamManager>().enabled = false;
            GetComponent<SmoothFollow>().enabled = false;
            GetComponent<MouseOrbit>().enabled = false;
            Camera.main.transform.rotation = Quaternion.Euler(Camera.main.transform.rotation.x, 330, Camera.main.transform.rotation.z);
        }

标签: admobinterstitial

解决方案


阅读这里的文档,可以为您省去很多麻烦。

您基本上必须使用您可以在 AdMob 控制台中创建的广告单元 ID 创建一个插页式广告。

this.interstitial = new InterstitialAd(adUnitId);

然后,您需要创建一个 AdRequest 并使用它来加载这样的插页式广告:

AdRequest request = new AdRequest.Builder().Build();
this.interstitial.LoadAd(request);

此时,您的插页式广告尚未出现在屏幕上。为此,您必须检查广告是否已加载,然后调用 Show 方法,例如:

  if (this.interstitial.IsLoaded()) {
    this.interstitial.Show();
  }

这里的要点是,您应该先加载广告 (LoadAd(request)) 一段时间,然后再决定展示它,以便广告有足够的时间实际加载。因此,在您的情况下,您可以在启动应用/游戏后不久加载插页式广告,并在单击按钮时调用 Show()。

请注意,有时没有加载广告,因为 AdMob 无法在请求的时间提供广告(出于各种原因)。此外,当您测试您的广告时,请使用 TestAds 进行测试,以免您的 AdMob 帐户被暂停。

当需要隐藏插页式广告时,不要忘记调用:

interstitial.Destroy();

插页式广告生命周期中的各种事件也有可用的回调,您可以在提供的文档页面中阅读所有内容。


推荐阅读