admob - 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);
}
解决方案
阅读这里的文档,可以为您省去很多麻烦。
您基本上必须使用您可以在 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();
插页式广告生命周期中的各种事件也有可用的回调,您可以在提供的文档页面中阅读所有内容。
推荐阅读
- javascript - firebase 抛出错误未捕获错误:Reference.child 失败:
- spring - 为 Spring Boot 应用程序设置系统负载平均值警报
- android - Mockito - 模拟类变量为空
- linux - 为什么使用共享内存和信号量比使用管道更快?
- python - AWS p3.2xlarge 上的图像分割推理比本地 2080 TI 慢
- r - 从R中的单列创建从节点表
- node.js - 黄瓜中的 grep --dry-run 输出
- node.js - NodeJs 在使用 SSL 证书连接到在 Windows 服务器上运行的 Web API 时引发 SSL 错误
- python-3.x - 如何在烧瓶服务器中启动 webdriver 实例并在全局范围内使用它
- azure - Azure 托管标识是否足够?