在 Unity 应用中添加插屏广告

使用 Audience Network,您的 Android 和 iOS 应用可以通过展示 Facebook 广告实现变现。插屏广告是一种全屏广告体验,可用于应用流程中的切换点,例如在不同活动之间,或者在游戏中不同关卡之间的暂停时段。其创意内容可以是图片、视频或轮播。本指南将为您介绍如何在应用中添加插屏广告。

请确保先阅读 Audience Network 入门指南Unity 入门指南,然后再继续操作。

添加插屏广告的步骤

第 1 步:创建插屏广告对象

第 2 步:添加回调事件

第 3 步:加载广告

第 4 步:显示广告

第 1 步:创建插屏广告对象

如要显示插屏广告,首先要在附加到 GameObject 的 C# 脚本中创建一个 InterstitialAd 对象。

...
using AudienceNetwork;
...

public class InterstitialAdTest : MonoBehaviour
{
    ...
    private InterstitialAd interstitialAd;
    private bool isLoaded;
    ...

    public void LoadInterstitial()
    {
        this.interstitialAd = new InterstitialAd("YOUR_PLACEMENT_ID");
        this.interstitialAd.Register(this.gameObject);

        // Set delegates to get notified on changes or when the user interacts with the ad.
        this.interstitialAd.InterstitialAdDidLoad = (delegate() {
            Debug.Log("Interstitial ad loaded.");
            this.isLoaded = true;
        });
        interstitialAd.InterstitialAdDidFailWithError = (delegate(string error) {
            Debug.Log("Interstitial ad failed to load with error: " + error);
        });
        interstitialAd.InterstitialAdWillLogImpression = (delegate() {
            Debug.Log("Interstitial ad logged impression.");
        });
        interstitialAd.InterstitialAdDidClick = (delegate() {
            Debug.Log("Interstitial ad clicked.");
        });

        this.interstitialAd.interstitialAdDidClose = (delegate() {
            Debug.Log("Interstitial ad did close.");
            if (this.interstitialAd != null) {
                this.interstitialAd.Dispose();
            }
        });

        // Initiate the request to load the ad.
        this.interstitialAd.LoadAd();
    }
    ...
}

InterstitialAd 的构造函数包含以下参数:

  • placementId - 这个插屏广告单元的 Audience Network 版位编号。

第 2 步:添加回调事件

然后,您可以执行几次回调,以订阅广告的生命周期事件。为事件注册委派,聆听是否有这些事件,如下列示例所示:

...
// Set delegates to get notified on changes or when the user interacts with the ad.
this.interstitialAd.InterstitialAdDidLoad = (delegate() {
    Debug.Log("Interstitial ad loaded.");
    this.isLoaded = true;
});
interstitialAd.InterstitialAdDidFailWithError = (delegate(string error) {
    Debug.Log("Interstitial ad failed to load with error: " + error);
});
interstitialAd.InterstitialAdWillLogImpression = (delegate() {
    Debug.Log("Interstitial ad logged impression.");
});
interstitialAd.InterstitialAdDidClick = (delegate() {
    Debug.Log("Interstitial ad clicked.");
});

this.interstitialAd.interstitialAdDidClose = (delegate() {
    Debug.Log("Interstitial ad did close.");
    if (this.interstitialAd != null) {
        this.interstitialAd.Dispose();
    }
});
...

Unity Android 中的已终止广告活动回调

相关内容只适用于 Android。

目前,Unity Android 游戏只支持主要的 Unity Activity 拥有 singleTasklaunchMode。请参阅 Unity Android 清单文档Android 活动文档

由于我们使用 Activity 来显示 InterstitialRewarded Video 广告,如果用户在后台运行应用,然后使用图标(而非应用切换器)重新启动此应用,则广告活动可能会在没有正确关闭的情况下遭到终止。您可以使用下列回调,检查用户是否关闭了广告:

适用于插屏广告:

this.interstitialAd.interstitialAdDidClose = (delegate() { Debug.Log("Interstitial ad did close."); this.didClose = true; if (this.interstitialAd != null) { this.interstitialAd.Dispose(); } }); #if UNITY_ANDROID /* * Only relevant to Android. * This callback will only be triggered if the Interstitial activity has * been destroyed without being properly closed.This can happen if an * app with launchMode:singleTask (such as a Unity game) goes to * background and is then relaunched by tapping the icon. */ this.interstitialAd.interstitialAdActivityDestroyed = (delegate() { if (!this.didClose) { Debug.Log("Interstitial activity destroyed without being closed first."); Debug.Log("Game should resume."); } }); #endif 

适用于奖励式视频广告:

this.rewardedVideoAd.rewardedVideoAdDidClose = (delegate() { Debug.Log("Rewarded video ad did close."); this.didClose = true; if (this.rewardedVideoAd != null) { this.rewardedVideoAd.Dispose(); } }); #if UNITY_ANDROID /* * Only relevant to Android. * This callback will only be triggered if the Rewarded Video activity * has been destroyed without being properly closed.This can happen if * an app with launchMode:singleTask (such as a Unity game) goes to * background and is then relaunched by tapping the icon. */ this.rewardedVideoAd.rewardedVideoAdActivityDestroyed = (delegate() { if (!this.didClose) { Debug.Log("Rewarded video activity destroyed without being closed first."); Debug.Log("Game should resume. User should not get a reward."); } }); #endif 

第 3 步:加载广告

在对 InterstitialAd 进行实例化之后,下一步骤就是加载广告。您可以使用 InterstitialAd 类别的 loadAd() 方法完成这项操作。

针对上述示例,广告加载方法如下所述:

...
this.interstitialAd.LoadAd();
...

第 4 步:显示广告

最后,广告加载完成后,您可以调用 Show 方法将插屏广告呈现在屏幕上。例如,您可以为 ShowInterstitial 创建一个函数,并在显示广告时调用该函数:

// Show button
public void ShowInterstitial()
{
    if (this.isLoaded) {
        this.interstitialAd.Show();
        this.isLoaded = false;

    } else {
        Debug.Log("Interstitial Ad not loaded!");
    }
}

后续步骤

按照我们的指南操作,在 Unity 应用中集成不同的广告格式:

准备好让您的应用上线并开始创收后,请先确保应用符合 Audience Network 政策Facebook 社群标准,然后提交应用供审核