开屏广告

开屏广告是一种特殊的广告格式,适合希望创收的发布商 应用加载屏幕开屏广告可以随时关闭 。

开屏广告会自动在一个较小的区域内展示品牌信息,让用户知道他们是在 。开屏广告的外观示例如下:

前提条件

  • 完成入门指南
  • Unity 插件 7.1.0 或更高版本。

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

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

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

/21775744923/example/app-open

实现

植入开屏广告的主要步骤如下:

  1. 创建实用程序类
  2. 加载开屏广告
  3. 监听开屏广告事件
  4. 考虑广告有效期
  5. 监听应用状态事件
  6. 展示开屏广告
  7. 清理开屏广告
  8. 预加载下一个开屏广告

创建实用程序类

创建一个名为 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
{

    // This ad unit is configured to always serve test ads.
    private string _adUnitId = "/21775744923/example/app-open";

    public bool IsAdAvailable
    {
        get
        {
            return _appOpenAd != null;
        }
    }

    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        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()
    {
    }
}

加载开屏广告

开屏广告的加载是通过在Load() AppOpenAd 类。该加载方法需要使用广告单元 ID、 AdManagerAdRequest 对象,以及一个完成处理程序, 会在广告加载成功或失败时调用。加载的 AppOpenAd 对象为 完成处理程序中作为参数提供。以下示例显示了 以加载 AppOpenAd


  // This ad unit is configured to always serve test ads.
  private string _adUnitId = "/21775744923/example/app-open";

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

      // 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);
          });
  }

监听开屏广告事件

要进一步自定义您的广告行为,您可以加入许多 事件:打开、关闭等。监听 注册 delegate 以控制这些事件,如下所示。

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 -= OnAppStateChanged;
}

当我们处理 AppState.Foreground 状态和 IsAdAvailable 时 为 true,我们调用 ShowAppOpenAd() 来展示广告。

private void OnAppStateChanged(AppState state)
{
    Debug.Log("App State changed to : "+ state);

    // if the app is Foregrounded and the ad is available, show it.
    if (state == AppState.Foreground)
    {
        if (IsAdAvailable)
        {
            ShowAppOpenAd();
        }
    }
}

展示开屏广告

若要展示已加载的开屏广告,请对Show() AppOpenAd 实例。每次加载时,广告仅可展示一次。使用 CanShowAd() 方法,以验证广告是否已准备好进行展示。

/// <summary>
/// Shows the app open ad.
/// </summary>
public void ShowAppOpenAd()
{
    if (appOpenAd != null && appOpenAd.CanShowAd())
    {
        Debug.Log("Showing app open ad.");
        appOpenAd.Show();
    }
    else
    {
        Debug.LogError("App open ad is not ready yet.");
    }
}

清理开屏广告

完成 AppOpenAd 后,请务必调用 Destroy() 方法:

appOpenAd.Destroy();

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

预加载下一个开屏广告

AppOpenAd 是一次性对象。这意味着,开屏广告展示后 无法再次使用该对象。若要再请求一个开屏广告,请执行以下操作: 您需要创建一个新的 AppOpenAd 对象。

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

private void RegisterReloadHandler(AppOpenAd ad)
{
    ...
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("App open ad full screen content closed.");

        // Reload the ad so that we can show another as soon as possible.
        LoadAppOpenAd();
    };
    // 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);

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

冷启动和加载屏幕

到目前为止,本文档假定您仅在用户 让应用在内存中挂起时进入前台运行“冷启动”出现以下情况: 您的应用已启动,但之前未在内存中挂起。

冷启动的一个示例是用户首次打开您的应用。 采用冷启动时,您将无法在之前加载的开屏广告 立即展示从您请求广告到收到广告之间的延迟时间 可能会导致用户能够在使用应用之前 用户意外看到与广告内容不符的广告应该避免这种情况 用户体验不佳。

若要在冷启动时使用开屏广告,首选方式是使用加载屏幕 来加载游戏或应用素材资源,并且仅在加载时展示广告 屏幕。如果您的应用已加载完毕,并且用户已经转至主应用, 不要展示广告。

最佳做法

借助开屏广告,您可以在应用首次加载时通过应用的加载屏幕创收 启动和应用切换期间,但重要的是始终遵循以下 确保用户享受使用应用的乐趣

  • 在用户使用几次您的应用后展示您的第一个开屏广告。
  • 在用户等待的时段展示开屏广告 加载您的应用
  • 如果开屏广告和加载屏幕下方有加载屏幕 广告加载完毕之前,请关闭加载屏幕 OnAdDidDismissFullScreenContent 事件处理脚本。
  • 在 iOS 平台上,AppStateEventNotifier 会实例化 AppStateEventClient GameObject。必须填写此GameObject,才能参加活动 别毁了它如果 GameObject: 已销毁。

其他资源