原生重叠式广告

开始使用

原生重叠式广告通过原生的界面组件呈现给用户 平台这些广告会以重叠式广告的形式展示在 应用。这与横幅广告的运作方式类似 自定义广告的外观。

原生重叠式广告支持中介广告和视频广告。这是一项关键优势 原生重叠式广告相对于原生广告

本指南介绍了如何在 Unity 应用中植入原生重叠式广告,以及 一些需要考虑的重要事项

前提条件

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

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

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

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

/6499/example/native

加载原生重叠式广告

原生重叠式广告的加载是使用静态 Load() 方法在 NativeOverlayAd 类。已加载的 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;
    }
}