开屏广告是一种特殊的广告格式,适合希望通过应用加载屏幕创收的发布商。开屏广告在用户将您的应用切换为在前台运行时展示,用户可随时关闭。
开屏广告会自动在一个较小的区域内展示您的品牌信息,让用户一眼就知道他们正在您的应用中。以下是一个开屏广告示例:

前提条件
在继续操作之前,请先完成以下事项:
- 设置 Google Mobile Ads Unity Plugin。
- 安装 Unity 插件 7.1.0 或更高版本。
务必用测试广告进行测试
以下示例代码包含一个广告单元 ID,可供您用来请求测试广告。该测试广告单元 ID 已经过专门配置,可为每个请求返回测试广告(而不是实际投放的广告),因此能够安全地使用。
不过,您在 AdMob 网页界面中注册应用,并创建要在应用中使用的广告单元 ID 后,需要在开发期间明确将您的设备配置为测试设备。
Android
ca-app-pub-3940256099942544/1033173712
iOS
ca-app-pub-3940256099942544/4411468910
实现
植入开屏广告的主要步骤如下所示:
- 创建实用工具类
- 加载开屏广告
- 监听开屏广告事件
- 考虑广告有效期
- 监听应用状态事件
- 展示开屏广告
- 清理开屏广告
- 预加载下一个开屏广告
创建实用工具类
创建一个名为 AppOpenAdController 的新类,以用于加载广告。此类会控制一个实例变量,以便跟踪各个平台加载的广告和广告单元 ID。
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
/// <summary>
/// Demonstrates how to use the Google Mobile Ads app open ad format.
/// </summary>
[AddComponentMenu("GoogleMobileAds/Samples/AppOpenAdController")]
public class AppOpenAdController : MonoBehaviour
{
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/9257395921";
#elif UNITY_IPHONE
string _adUnitId = "ca-app-pub-3940256099942544/5575463023";
#else
private string _adUnitId = "unused";
#endif
public bool IsAdAvailable
{
get
{
return _appOpenAd != null;
}
}
public void Start()
{
// Initialize Google Mobile Ads Unity Plugin.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
/// <summary>
/// Loads the app open ad.
/// </summary>
public void LoadAppOpenAd()
{
}
/// <summary>
/// Shows the app open ad.
/// </summary>
public void ShowAppOpenAd()
{
}
}
加载开屏广告
开屏广告的加载是使用 AppOpenAd 类中的静态 Load() 方法完成的。该加载方法需要使用广告单元 ID、AdRequest 对象,以及在广告加载成功或失败时调用的完成处理程序。所加载的 AppOpenAd 对象以该完成处理程序中的一个参数形式提供。以下示例展示了如何加载 AppOpenAd。
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/9257395921";
#elif UNITY_IPHONE
string _adUnitId = "ca-app-pub-3940256099942544/5575463023";
#else
private string _adUnitId = "unused";
#endif
private AppOpenAd appOpenAd;
/// <summary>
/// Loads the app open ad.
/// </summary>
public void LoadAppOpenAd()
{
// Clean up the old ad before loading a new one.
if (appOpenAd != null)
{
appOpenAd.Destroy();
appOpenAd = null;
}
Debug.Log("Loading the app open ad.");
// Create our request used to load the ad.
var adRequest = new AdRequest();
// send the request to load the ad.
AppOpenAd.Load(_adUnitId, adRequest,
(AppOpenAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("app open ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("App open ad loaded with response : "
+ ad.GetResponseInfo());
appOpenAd = ad;
RegisterEventHandlers(ad);
});
}
监听开屏广告事件
若要进一步自定义您广告的行为,您可以将代码与广告生命周期中的一些事件(如打开、关闭等)挂钩。具体可通过注册一个代理来监听这些事件,如下所示。
private void RegisterEventHandlers(AppOpenAd ad)
{
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("App open ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("App open ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
ad.OnAdClicked += () =>
{
Debug.Log("App open ad was clicked.");
};
// Raised when an ad opened full screen content.
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("App open ad full screen content opened.");
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("App open ad full screen content closed.");
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("App open ad failed to open full screen content " +
"with error : " + error);
};
}
考虑广告有效期
为确保您不会展示过期的广告,请在 AppOpenAdController 中添加一个方法,用于检查广告加载后经过了多长时间。这样,就可以使用该方法检查广告是否仍然有效。
开屏广告的超时设置为 4 小时。您可以在 _expireTime 变量中缓存加载时间。
// send the request to load the ad.
AppOpenAd.Load(_adUnitId, adRequest,
(AppOpenAd ad, LoadAdError error) =>
{
// If the operation failed, an error is returned.
if (error != null || ad == null)
{
Debug.LogError("App open ad failed to load an ad with error : " +
error);
return;
}
// If the operation completed successfully, no error is returned.
Debug.Log("App open ad loaded with response : " + ad.GetResponseInfo());
// App open ads can be preloaded for up to 4 hours.
_expireTime = DateTime.Now + TimeSpan.FromHours(4);
_appOpenAd = ad;
});
更新 IsAdAvailable 属性以检查 _expireTime,即可确认所加载的广告是否仍然有效。
public bool IsAdAvailable
{
get
{
return _appOpenAd != null
&& _appOpenAd.IsLoaded()
&& DateTime.Now < _expireTime;
}
}
监听应用状态事件
使用 AppStateEventNotifier 可监听应用前台和后台事件。每当应用处于前台或后台时,此类都会引发 AppStateChanged 事件。
private void Awake()
{
// Use the AppStateEventNotifier to listen to application open/close events.
// This is used to launch the loaded ad when we open the app.
AppStateEventNotifier.AppStateChanged += OnAppStateChanged;
}
private void OnDestroy()
{
// Always unlisten to events when complete.
AppStateEventNotifier.AppStateChanged -=