插页式广告

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

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

前提条件

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

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

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

/6499/example/interstitial

初始化移动广告 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、 AdManagerAdRequest 对象,以及一个完成处理程序, 会在广告加载成功或失败时调用。加载的 AdManagerInterstitialAd 对象以参数的形式在 完成处理程序。以下示例展示了如何加载 AdManagerInterstitialAd.


  // This ad unit is configured to always serve test ads.
  private string _adUnitId = "/6499/example/interstitial";

  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 AdManagerAdRequest();

      // send the request to load the ad.
      AdManagerInterstitialAd.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() AdManagerInterstitialAd 实例。广告每展示一次 加载。使用 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);
    };
}

清理插页式广告

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

_interstitialAd.Destroy();

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

预加载下一个插页式广告

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

要为下一次展示机会准备好插页式广告,请预加载 展示 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();
    };
}

应用事件

借助应用事件,您可以制作能够向应用代码发送消息的广告。应用 然后根据这些消息采取相应措施

您可以使用 AppEvent 监听 Ad Manager 所特有的应用事件。这些活动 可能发生在广告生命周期内的任何时间,甚至是调用 load 之前。

namespace GoogleMobileAds.Api.AdManager;

/// The App event message sent from the ad.
public class AppEvent
{
    // Name of the app event.
    string Name;
    // Argument passed from the app event.
    string Value;
}

OnAppEventReceived 在广告中发生应用事件时引发。这里有一个 在代码中处理此事件的示例:

_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
    Debug.Log($"Received app event from the ad: {args.Name}, {args.Value}.");
};

以下示例展示了如何更改应用的背景颜色 具体取决于具有颜色名称的应用事件:

_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
  if (args.Name == "color")
  {
    Color color;
    if (ColorUtility.TryParseColor(arg.Value, out color))
    {
      gameObject.GetComponent<Renderer>().material.color = color;
    }
  }
};

另外,下面是发送颜色应用事件的相应广告素材:

<html>
<head>
  <script src="//www.gstatic.com/afma/api/v1/google_mobile_app_ads.js"></script>
  <script>
    document.addEventListener("DOMContentLoaded", function() {
      // Send a color=green event when ad loads.
      admob.events.dispatchAppEvent("color", "green");

      document.getElementById("ad").addEventListener("click", function() {
        // Send a color=blue event when ad is clicked.
        admob.events.dispatchAppEvent("color", "blue");
      });
    });
  </script>
  <style>
    #ad {
      width: 320px;
      height: 50px;
      top: 0px;
      left: 0px;
      font-size: 24pt;
      font-weight: bold;
      position: absolute;
      background: black;
      color: white;
      text-align: center;
    }
  </style>
</head>
<body>
  <div id="ad">Carpe diem!</div>
</body>
</html>

最佳做法

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

其他资源