unity3d - 广告未在 Game Unity 上展示
问题描述
我已使用以下代码在我的统一游戏上放置横幅广告,但我的广告既没有显示测试广告,也没有显示实际的 admob 广告。我不知道发生了什么,我也在 admob 中完成了我的付款详细信息。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
public class AdmobScript : MonoBehaviour
{
public string BannerId;
void Start()
{
RequestBanner();
}
private void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = BannerId;
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the bottom of the screen.
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner,
AdPosition.Bottom);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
bannerView.LoadAd(request);
}
}
解决方案
您的代码中缺少一个函数来处理bannerView 对象的显示功能:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
public class AdmobScript : MonoBehaviour
{
BannerView bannerView;
void Start()
{
RequestBanner();
}
private void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = BannerId;
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the bottom of the screen.
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner,
AdPosition.Bottom);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
bannerView.LoadAd(request);
// Handle the show functionality of banner ads
bannerView.OnAdLoaded+=HandleOnAdLoaded;
}
void HandleOnAdLoadeded(object a, EventArgs args) {
print("loaded");
bannerView.Show();
}
}
欲了解更多信息:
推荐阅读
- php - 解析符号字符串对以构建 sql 的 WHERE 子句的条件表达式
- jenkins - 我正在尝试在 jenkins 上运行 selenium 脚本,但出现以下错误(我还附上了我要运行的脚本)
- python - 如何将抓取的数据添加到我的列表中,我无法打印“ranking_list”并且它不能为空
- mapbox - MapBox中的集群标记,如何“累积”不同的属性?
- java - 导入 com.google.android.gms.security.ProviderInstaller
- javascript - 如何在useEffect()中刷新signalR的连接ID?
- bash - 从执行 ssh 两级的 shell 脚本获取输出
- google-cloud-platform - GCP 负载均衡器 URL 映射将所有请求路由到默认后端服务,配置根据文档显示正确
- macos - 如何使用 AppleScript 及其脚本标签替换 Pages 中的单个占位符文本?
- reactjs - 仅在 React 中使用 ContextAPI 加载查找值一次