插页式广告

插页式广告是全屏广告,会覆盖所在应用的整个界面。 它们通常在应用流程的自然过渡点展示 例如在游戏关卡之间暂停时。当应用显示 插页式广告,用户可以选择点按广告并继续 或者将其关闭并返回应用。 案例研究

本指南介绍了如何将插页式广告植入到 Unity 应用中。

前提条件

始终使用测试广告进行测试

以下示例代码包含一个广告单元 ID,可用于请求 测试广告。该测试广告单元已经过专门配置 会返回测试广告 制作出来的广告,确保安全使用。

不过,当您在 AdMob 网页界面并创建您自己的广告单元 要在您的应用中使用的 ID,请明确将设备配置为测试 设备 开发。

Android

ca-app-pub-3940256099942544/1033173712

iOS

ca-app-pub-3940256099942544/4411468910

初始化移动广告 SDK

加载广告之前,请先调用 MobileAds.Initialize()。此操作仅需执行一次,最好是在应用启动时执行。

using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }
}

如果您使用的是中介功能,请等到回调发生后再按 这样可确保初始化所有中介适配器

实现

植入插页式广告的主要步骤如下所示:

  1. 加载插页式广告
  2. 展示插页式广告
  3. 监听插页式广告事件
  4. 清理插页式广告
  5. 预加载下一个插页式广告

加载插页式广告

插页式广告的加载是通过在Load() InterstitialAd 类。该加载方法需要使用广告单元 ID、 AdRequest 对象,以及一个完成处理程序, 会在广告加载成功或失败时调用。加载的 InterstitialAd 对象以参数的形式在 完成处理程序。以下示例展示了如何加载 InterstitialAd


  // These ad units are configured to always serve test ads.
  #if UNITY_ANDROID
  private string _adUnitId = "ca-app-pub-3940256099942544/1033173712";
    #elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-3940256099942544/4411468910";
    #else
  private string _adUnitId = "unused";
    #endif

  private InterstitialAd _interstitialAd;

  /// <summary>
  /// Loads the interstitial ad.
  /// </summary>
  public void LoadInterstitialAd()
  {
      // Clean up the old ad before loading a new one.
      if (_interstitialAd != null)
      {
            _interstitialAd.Destroy();
            _interstitialAd = null;
      }

      Debug.Log("Loading the interstitial ad.");

      // create our request used to load the ad.
      var adRequest = new AdRequest();

      // send the request to load the ad.
      InterstitialAd.Load(_adUnitId, adRequest,
          (InterstitialAd ad, LoadAdError error) =>
          {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
              {
                  Debug.LogError("interstitial ad failed to load an ad " +
                                 "with error : " + error);
                  return;
              }

              Debug.Log("Interstitial ad loaded with response : "
                        + ad.GetResponseInfo());

              _interstitialAd = ad;
          });
  }

展示插页式广告

要展示已加载的插页式广告,请对Show() InterstitialAd 实例。广告每展示一次 加载。使用 CanShowAd() 方法验证广告是否已做好展示准备。

/// <summary>
/// Shows the interstitial ad.
/// </summary>
public void ShowInterstitialAd()
{
    if (_interstitialAd != null && _interstitialAd.CanShowAd())
    {
        Debug.Log("Showing interstitial ad.");
        _interstitialAd.Show();
    }
    else
    {
        Debug.LogError("Interstitial ad is not ready yet.");
    }
}

监听插页式广告事件

要进一步自定义您的广告行为,您可以加入许多 触发这些事件通过注册代理来监听这些事件 如下所示。

private void RegisterEventHandlers(InterstitialAd interstitialAd)
{
    // Raised when the ad is estimated to have earned money.
    interstitialAd.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log(String.Format("Interstitial ad paid {0} {1}.",
            adValue.Value,
            adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    interstitialAd.OnAdImpressionRecorded += () =>
    {
        Debug.Log("Interstitial ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    interstitialAd.OnAdClicked += () =>
    {
        Debug.Log("Interstitial ad was clicked.");
    };
    // Raised when an ad opened full screen content.
    interstitialAd.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("Interstitial ad full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    interstitialAd.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("Interstitial ad full screen content closed.");
    };
    // Raised when the ad failed to open full screen content.
    interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Interstitial ad failed to open full screen content " +
                       "with error : " + error);
    };
}

清理插页式广告

完成 InterstitialAd 后, 请务必先调用 Destroy() 方法,然后再放弃对它的引用:

_interstitialAd.Destroy();

这会通知插件该对象已不再使用,以及 可收回。此方法调用失败会导致内存泄漏。

预加载下一个插页式广告

插页式广告是一次性的对象。也就是说,一旦插页式广告 便无法再次使用该对象。若要请求另一个插页式广告 创建一个新的 InterstitialAd 对象。

要为下一次展示机会准备好插页式广告,请预加载 展示 OnAdFullScreenContentClosed 或 引发了 OnAdFullScreenContentFailed 广告事件。

private void RegisterReloadHandler(InterstitialAd interstitialAd)
{
    // Raised when the ad closed full screen content.
    interstitialAd.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("Interstitial Ad full screen content closed.");

        // Reload the ad so that we can show another as soon as possible.
        LoadInterstitialAd();
    };
    // Raised when the ad failed to open full screen content.
    interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Interstitial ad failed to open full screen content " +
                       "with error : " + error);

        // Reload the ad so that we can show another as soon as possible.
        LoadInterstitialAd();
    };
}

最佳做法

确定插页式广告是否适合您的应用。
在具有自然过渡点的应用中,插页式广告的效果最好。 应用内任务的结束,例如分享图片或完成一项任务 游戏关卡,就会产生这样一个点。请务必考虑 应用如何才能最有效地展示插页式广告,以及用户可能会如何响应。
在展示插页式广告时暂停操作。
插页式广告类型多样,例如文字广告、 图片或视频务必要确保您的应用在显示 但它也会暂停使用某些资源 请充分利用它们例如,当您调用 插页式广告时,请务必暂停应用产生的所有音频输出。 您可以在 OnAdFullScreenContentClosed() 事件中继续播放声音, 在用户完成与广告的互动后即可调用该方法。在 此外,请考虑暂时停止所有密集计算任务,例如 并在展示广告时加载游戏循环。这可确保用户 遇到图像无响应、响应慢或视频卡顿的现象。
不要向用户展示太多广告。
虽然提高插页式广告在应用中的展示频次似乎 例如增加收入的好方法,但这也会影响用户体验 点击率也会降低确保用户不会过于频繁地 打断了他们,导致他们无法继续使用您的应用。

其他资源

。 * 示例用例