插屏广告一般采用全屏或整页形式,会暂时覆盖应用的整个界面,通常出现在应用自然的暂停或过渡节点,例如游戏关卡完成后、主要视图切换时等。
以下各节将向您介绍如何加载和显示插屏广告。
以下代码展示了如何绑定委托以及加载第一则插屏广告:

// UMyWidget.cpp (UMyWidget inherits from UObject to access TimerManager)
#include "UMyWidget.h"
#include "AppLovinMAX.h"
// For retry timer logic
#include "Async/Async.h"
#include "Engine/World.h"
#include "TimerManager.h"
const FString InterstitialAdUnitId = TEXT("«ad-unit-ID»");
int RetryAttempt = 0;
FTimerHandle LoadTimerHandle;
void UMyWidget::InitializeInterstitialAds(){
// Bind member functions to delegates
UAppLovinMAX::OnInterstitialAdLoadedDelegate.AddUObject(this, &UMyWidget::OnInterstitialAdLoaded);
UAppLovinMAX::OnInterstitialAdLoadFailedDelegate.AddUObject(this, &UMyWidget::OnInterstitialAdLoadFailed);
UAppLovinMAX::OnInterstitialAdDisplayFailedDelegate.AddUObject(this, &UMyWidget::OnInterstitialAdDisplayFailed);
UAppLovinMAX::OnInterstitialAdHiddenDelegate.AddUObject(this, &UMyWidget::OnInterstitialAdHidden);
// Load first interstitial
LoadInterstitial();
}
void UMyWidget::LoadInterstitial(){
UAppLovinMAX::LoadInterstitial(InterstitialAdUnitId);
}
void UMyWidget::OnInterstitialAdLoaded(const FAdInfo &AdInfo){
// Interstitial ad is ready to be shown. UAppLovinMAX::IsInterstitialReady(InterstitialAdUnitId) will now return 'true'
// Reset retry attempt
RetryAttempt = 0;
}
void UMyWidget::OnInterstitialAdLoadFailed(const FAdInfo &AdInfo, const FAdError &AdError){
// Interstitial ad failed to load
// AppLovin recommends that you retry with exponentially higher delays, up to a maximum delay (in this case 64 seconds)
RetryAttempt++;
AsyncTask(ENamedThreads::GameThread, [this]() {
float RetryDelay = FMath::Pow(2.0f, FMath::Min(6, RetryAttempt));
GetWorld()->GetTimerManager().SetTimer(LoadTimerHandle, this, &UMyWidget::LoadInterstitial, RetryDelay, false);
});
}
void UMyWidget::OnInterstitialAdDisplayFailed(const FAdInfo &AdInfo, const FAdError &AdError){
// Interstitial ad failed to display. AppLovin recommends that you load the next ad.
LoadInterstitial();
}
void UMyWidget::OnInterstitialAdHidden(const FAdInfo &AdInfo){
// Interstitial ad is hidden. Pre-load the next ad.
LoadInterstitial();
}最佳实践:展示插屏广告的最佳时机
调用 ShowInterstitial() 以显示插屏广告:

if (UAppLovinMAX::IsInterstitialReady(InterstitialAdUnitId))
{
UAppLovinMAX::ShowInterstitial(InterstitialAdUnitId);
}