Unity当按钮点击显示广告插页式

时间:2020-06-24 13:24:28

标签: admob interstitial

我想添加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);
        }

1 个答案:

答案 0 :(得分:0)

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

基本上,您必须使用可以在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();

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