Native overlay ads

原生重叠式广告通过平台原生的界面组件向用户展示。这些广告以叠加层的形式显示在应用之上。这与横幅广告的运作方式类似,但可以自定义广告的外观。

原生重叠式广告支持中介和视频广告。这是原生重叠式广告相对于原生广告的一个主要优势。

本指南将向您介绍如何在 Unity 应用中植入原生重叠式广告,以及在此过程中需要注意的一些重要事项。

前提条件

  • 通读入门指南
  • Unity 插件 9.0.0 或更高版本。

Always test with test ads

The following sample code contains an ad unit ID which you can use to request test ads. It's been specially configured to return test ads rather than production ads for every request, making it safe to use.

However, after you've registered an app in the Ad Manager web interface and created your own ad unit IDs for use in your app, explicitly configure your device as a test device during development.

/6499/example/native

加载原生重叠式广告

原生重叠式广告的加载是通过对 NativeOverlayAd 类使用静态 Load() 方法完成的。已加载的 NativeOverlayAd 对象会以完成处理程序中的参数的形式提供。

以下代码使用 NativeOverlayAd 加载广告:




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



private NativeOverlayAd _nativeOverlayAd;

/// <summary>
/// Loads the ad.
/// </summary>
public void LoadAd()
{
    // Clean up the old ad before loading a new one.
    if (_nativeOverlayAd != null)
    {
        DestroyAd();
    }

    Debug.Log("Loading native overlay ad.");

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

    // Optional: Define native ad options.
    var options = new NativeAdOptions
    {
        AdChoicesPosition = AdChoicesPlacement.TopRightCorner,
        MediaAspectRatio = NativeMediaAspectRatio.Any,
    };

    // Send the request to load the ad.
    NativeOverlayAd.Load(_adUnitId, adRequest, options,
        (NativeOverlayAd ad, LoadAdError error) =>
    {
        if (error != null)
        {
            Debug.LogError("Native Overlay ad failed to load an ad " +
                           " with error: " + error);
            return;
        }

        // The ad should always be non-null if the error is null, but
        // double-check to avoid a crash.
        if (ad == null)
        {
            Debug.LogError("Unexpected error: Native Overlay ad load event " +
                           " fired with null ad and null error.");
            return;
        }

        // The operation completed successfully.
        Debug.Log("Native Overlay ad loaded with response : " +
                   ad.GetResponseInfo());
        _nativeOverlayAd = ad;

        // Register to ad events to extend functionality.
        RegisterEventHandlers(ad);
    });
}

呈现原生重叠式广告并为其设置样式

原生重叠式广告使用 NativeTemplateStyle 呈现。该类可定义用于自定义广告外观的字段。

TemplateID 是必需字符串,用于定义用于呈现原生重叠式广告的原生模板。您可以使用 NativeTemplateID 常量为您的广告选择合适的原生广告模板。

以下代码使用中等模板和自定义样式呈现原生重叠式广告。

/// <summary>
/// Renders the ad.
/// </summary>
public void RenderAd()
{
    if (_nativeOverlayAd != null)
    {
        Debug.Log("Rendering Native Overlay ad.");

        // Define a native template style with a custom style.
        var style = new NativeTemplateStyle
        {
            TemplateID = NativeTemplateID.Medium,
            MainBackgroundColor = Color.red,
            CallToActionText = new NativeTemplateTextStyles
            {
                BackgroundColor = Color.green,
                FontColor = Color.white,
                FontSize = 9,
                Style = NativeTemplateFontStyle.Bold
            }
        };

        // Renders a native overlay ad at the default size
        // and anchored to the bottom of the screne.
        _nativeOverlayAd.RenderTemplate(style, AdPosition.Bottom);
    }
}

显示和隐藏原生重叠式广告

以下代码演示了如何展示已加载的原生重叠式广告。

/// <summary>
/// Shows the ad.
/// </summary>
public void ShowAd()
{
    if (_nativeOverlayAd != null)
    {
        Debug.Log("Showing Native Overlay ad.");
        _nativeOverlayAd.Show();
    }
}

隐藏原生重叠式广告

以下代码演示了如何隐藏原生重叠式广告。

/// <summary>
/// Hides the ad.
/// </summary>
public void HideAd()
{
    if (_nativeOverlayAd != null)
    {
        Debug.Log("Hiding Native Overlay ad.");
        _nativeOverlayAd.Hide();
    }
}

销毁原生重叠式广告

使用完原生重叠式广告后,请务必调用 Destroy() 以释放资源。

/// <summary>
/// Destroys the native overlay ad.
/// </summary>
public void DestroyAd()
{
    if (_nativeOverlayAd != null)
    {
        Debug.Log("Destroying native overlay ad.");
        _nativeOverlayAd.Destroy();
        _nativeOverlayAd = null;
    }
}