横幅广告

请选择平台Android iOS Unity Flutter

横幅广告视图是在屏幕上占据一处位置的矩形图片或文字广告。用户与应用互动时,这类广告会停留在屏幕上,并且可在一段时间后自动刷新。如果您刚开始接触移动广告,建议从横幅广告着手。

本指南介绍了如何将横幅广告视图植入到 Unity 应用中。除了代码段和操作说明之外,本指南还介绍了如何将横幅广告调整至合适的尺寸,并提供了其他资源的链接。

前提条件

务必用测试广告进行测试

以下示例代码包含一个广告单元 ID,可供您用来请求测试广告。该测试广告单元 ID 已经过专门配置,可为每个请求返回测试广告(而不是实际投放的广告),因此能够安全地使用。

不过,在 Ad Manager 网页界面中注册应用并创建要在应用中使用的广告单元 ID 后,您需要在开发期间明确地将您的设备配置为测试设备

/21775744923/example/adaptive-banner

初始化移动广告 SDK

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

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

如果您使用的是中介功能,请等到回调发生后再加载广告,因为这可确保初始化所有的中介适配器。

BannerView 示例

以下示例代码详细介绍了如何使用横幅广告视图。该示例首先创建一个横幅广告视图实例,使用 AdManagerAdRequest 将广告加载到该横幅广告视图中,然后通过处理生命周期事件来扩展其功能。

创建横幅广告视图

若要使用横幅广告视图,第一步是创建横幅广告视图的实例。

// Create a 320x50 banner at top of the screen.
adManagerBannerView = new AdManagerBannerView("AD_UNIT_ID", AdSize.Banner, AdPosition.Top);

AD_UNIT_ID 替换为您的广告单元 ID。

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

  • adUnitId:要加载的横幅广告的广告单元 ID。
  • AdSize:您要使用的横幅广告尺寸
  • AdPosition:应放置横幅广告视图的位置。

(可选)创建采用自定义位置的横幅广告视图

为了更好地控制横幅广告视图在屏幕上的位置(而不是采用 AdPosition 值提供的位置),请使用以 x 坐标和 y 坐标为参数的构造函数:

// Create a 320x50 banner views at coordinate (0,50) on screen.
adManagerBannerView = new AdManagerBannerView("AD_UNIT_ID", AdSize.Banner, 0, 50);

横幅视图的左上角会放置在传给构造函数的 x 值和 y 值所确定的位置,原点则是屏幕的左上角。

(可选)创建采用自定义尺寸的横幅广告视图

除了使用 AdSize 常量之外,您还可以为广告指定自定义尺寸:

// Create a 250x250 banner at the bottom of the screen.
AdSize adSize = new AdSize(250, 250);
adManagerBannerView = new AdManagerBannerView("AD_UNIT_ID", adSize, AdPosition.Bottom);

(可选)多个广告尺寸

Ad Manager 允许指定多个可在 AdManagerBannerView 中投放的广告尺寸。在 SDK 中实现此功能之前,请先创建一个订单项,并将其定位到与不同尺寸的广告素材关联的相同广告单元。

在您的应用中,将多个 AdSize 参数传递到 ValidAdSizes

// Create a 250x250 banner at the bottom of the screen.
adManagerBannerView = new AdManagerBannerView("AD_UNIT_ID", AdSize.Banner, AdPosition.Top);

// Add multiple ad sizes.
adManagerBannerView.ValidAdSizes = new List<AdSize>
{
    AdSize.Banner,
    new AdSize(120, 20),
    new AdSize(250, 250),
};

如果 AdManagerAdView 在刷新时会改变尺寸,布局应能够自动适应新的尺寸。AdManagerAdView 将默认采用第一个参数中传递的尺寸,直到返回下一个广告。

加载横幅广告

AdManagerBannerView 放置好后,接下来就该使用 AdManagerBannerView 类中的 LoadAd() 方法加载广告了。此方法需要一个 参数,其中包含运行时信息,例如定位信息、排除标签和发布商提供的 ID。

如需加载广告,请创建一个 AdManagerAdRequest 并将其传递给 LoadAd() 方法。

// Send a request to load an ad into the banner view.
adManagerBannerView.LoadAd(new AdManagerAdRequest());

监听横幅广告视图事件

若要自定义您广告的行为,您可以将代码与广告生命周期中的一些事件(如加载、打开或关闭)挂钩。若要监听这些事件,请注册代理:

adManagerBannerView.OnBannerAdLoaded += () =>
{
    // Raised when an ad is loaded into the banner view.
};
adManagerBannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
{
    // Raised when an ad fails to load into the banner view.
};
adManagerBannerView.OnAdPaid += (AdValue adValue) =>
{
    // Raised when the ad is estimated to have earned money.
};
adManagerBannerView.OnAdImpressionRecorded += () =>
{
    // Raised when an impression is recorded for an ad.
};
adManagerBannerView.OnAdClicked += () =>
{
    // Raised when a click is recorded for an ad.
};
adManagerBannerView.OnAdFullScreenContentOpened += () =>
{
    // Raised when an ad opened full screen content.
};
adManagerBannerView.OnAdFullScreenContentClosed += () =>
{
    // Raised when the ad closed full screen content.
};

销毁横幅广告视图

使用完横幅广告视图后,请务必调用 Destroy() 以释放资源。

if (adManagerBannerView != null)
{
    // Always destroy the banner view when no longer needed.
    adManagerBannerView.Destroy();
    adManagerBannerView = null;
}

大功告成!您的应用现在就可以展示横幅广告了。

刷新广告

如果您已将广告单元配置为定期刷新,则在广告加载失败时无需请求加载其他广告。Google 移动广告 SDK 会遵从您在 Ad Manager 界面中指定的任何刷新频率。如果您尚未启用刷新,请发出新的请求。如需详细了解广告单元刷新(例如如何设置刷新频率),请参阅移动应用内广告的刷新频率

下表列出了标准横幅广告尺寸:

尺寸(宽 x 高,以 dp 为单位) 说明 可用性 AdSize 常量
320x50 标准横幅广告 手机和平板电脑 BANNER
320x100 大型横幅广告 手机和平板电脑 LARGE_BANNER
300x250 IAB 中矩形 手机和平板电脑 MEDIUM_RECTANGLE
468x60 IAB 全尺寸横幅广告 平板电脑 FULL_BANNER
728x90 IAB 页首横幅广告 平板电脑 LEADERBOARD
提供的宽度 x 自适应高度 自适应横幅广告 手机和平板电脑 不适用
屏幕宽度 x 32|50|90 智能横幅广告 手机和平板电脑 SMART_BANNER
详细了解自适应横幅广告(旨在取代智能横幅广告)。

应用事件

借助应用事件,您可以制作能够向应用代码发送消息的广告。这样,应用便可以根据这些消息来进行操作。

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

当广告中发生应用事件时,会触发 OnAppEventReceived。以下示例展示了如何在代码中处理此事件:

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

以下示例展示了如何根据带颜色名称的应用事件更改应用的背景颜色:

adManagerBannerView.OnAppEventReceived += (AppEvent args) =>
{
    if (args.Name == "color")
    {
        Color color;
        if (ColorUtility.TryParseHtmlString(args.Data, out color))
        {
            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: 0;
      left: 0;
      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>

其他资源