Flutter 的间隙集成
使用 Unity LevelPlay SDK 在 Flutter app 中集成全屏插页式广告服务,支持静态和视频格式。
阅读时间5 分钟最后更新于 2 天前
LevelPlay Interstitial 是一个全屏广告单元,通常在 app 生命周期中的自然过渡点投放。Unity 支持静态和视频插页。
先决条件
- 确保已将 LevelPlay Flutter 插件正确集成到 app 中。
- 确保使用 LevelPlay Initialization API 初始化 SDK。
- 在 LevelPlay 后台中找到 AdUnitID。
创建 Interstitial 并注册到事件
在收到 回调后创建 interstitial 对象。
onInitSuccessLevelPlay SDK 会触发多个事件来通知您的 Interstitial 活动。要接收这些事件,请注册广告单元的监听器。确保在初始化 SDK 之前设置监听器以防止任何信息丢失。
final LevelPlayInterstitialAd _interstitialAd = LevelPlayInterstitialAd(adUnitId: [YOUR_AD_UNIT]);@overridevoid initState() { super.initState(); _interstitialAd.setListener(this);}
设置 Interstitial 监听器
在代码中实现这些,以便了解广告投放情况。
LevelPlayInterstitialAdListener- 建议的最佳做法是在加载插页式广告之前设置监听器。
- 每个插页式广告必须有自己的监听器实现。
- 回调在主线程上运行。
@override void onAdLoaded(LevelPlayAdInfo adInfo) { // Provided when the ad is successfully loaded } @override void onAdLoadFailed(LevelPlayAdError error) { // Provided when the ad fails to load.包含广告单元信息 } @override void onAdDisplayed(LevelPlayAdInfo adInfo) { // Provided when the ad is displayed.这相当于展示 } @override void onAdDisplayFailed(LevelPlayAdError error, LevelPlayAdInfo adInfo) { // Provided when the ad fails to be displayed } @override void onAdClicked(LevelPlayAdInfo adInfo) { // Provided when the user clicks on the ad } @override void onAdClosed(LevelPlayAdInfo adInfo) { // Provided when the ad is closed } @override void onAdInfoChanged(LevelPlayAdInfo adInfo) { // Provided when the ad info is updated.加载另一个广告后可用,并且包含更高的 CPM/速率 }
加载 Interstitial Ad
要加载插页式广告,请使用 。
loadAd_interstitialAd.loadAd();
显示插页式广告
在收到回调后,使用 显示插页式广告。
onAdLoadedshowAd- 共享 Activity 是必需的。
- 如果使用放置,请在 showAd API 中通道放置/位置名称,如下文 Placements 部分所示。
- 广告成功展示给用户后,可以通过重复加载步骤加载另一个广告。
// Show ad without placement _interstitialAd.showAd(); // Show ad with placement _interstitialAd.showAd(placement: [YOUR_PLACEMENT]);
检查 Ad is Ready
为了避免广告展示失败并确保广告正常运行,建议的最佳做法是在调用 API 之前使用以下 API:
showAd-
:如果广告已成功加载并且广告单元未设置上限,则返回 true,否则返回 false。
isAdReady -
:限制有效放置/位置时返回 true。如果放置/位置无效或未设置上限,此 API 将返回 false。
isPlacementCapped
// Check that ad is ready and that the placement is not capped if(_interstitialAd.isAdReady() && !LevelPlayInterstitialAd.isPlacementCapped([YOUR_PLACEMENT])) { _interstitialAd.showAd(placement:[YOUR_PLACEMENT]); }
广告位
Unity 支持在 LevelPlay 控制面板上设置插页的广告位节奏和上限。
如果为插页式广告服务设置了位置,请调用 方法为指定的放置/位置提供广告。
showAd// Check that ad is ready and that the placement is not capped if(_interstitialAd.isAdReady() && !LevelPlayInterstitialAd.isPlacementCapped([YOUR_PLACEMENT])) { _interstitialAd.showAd(placement:[YOUR_PLACEMENT]); }
插页式广告服务的完整实现示例
// Start of the widget...final LevelPlayInterstitialAd _interstitialAd = LevelPlayInterstitialAd(adUnitId:'YOUR_AD_UNIT_ID'); @override void initState() { super.initState(); _interstitialAd.setListener(this); _interstitialAd.loadAd(); } @override void onAdClicked(LevelPlayAdInfo adInfo) { // Implement your logic here... } @override void onAdClosed(LevelPlayAdInfo adInfo) { // Implement your logic here... } @override void onAdDisplayFailed(LevelPlayAdError error, LevelPlayAdInfo adInfo) { // Implement your logic here... } @override void onAdDisplayed(LevelPlayAdInfo adInfo) { // Implement your logic here... } @override void onAdInfoChanged(LevelPlayAdInfo adInfo) { // Implement your logic here... } @override void onAdLoadFailed(LevelPlayAdError error) { // Implement your logic here... } @override void onAdLoaded(LevelPlayAdInfo adInfo) { // Implement your logic here, for example showing the ad _interstitialAd.showAd(); } // Rest of the widget // End of widget...
LevelPlay Mediation 演示应用程序
Integration Demo 应用程序演示了如何在 app 中集成插页式广告单元 API。
验证与 Integration Test Suite 的集成。
后续步骤
请遵循 Flutter 集成指南来集成其他插页式广告网络或配置其他广告格式: