ネイティブ オーバーレイ広告

始める

ネイティブ オーバーレイ広告は、ネイティブ UI コンポーネントを通じてユーザーに表示されます 説明します。これらの広告はディスプレイ ネットワークの 説明します。これはバナー広告の機能と似ていますが、 広告の外観をカスタマイズできます。

ネイティブ オーバーレイ広告は、メディエーションと動画広告に対応しています。これは Google Cloud の ネイティブ オーバーレイ広告の上にネイティブ広告がある場合。

このガイドでは、Unity アプリにネイティブ オーバーレイ広告を実装する方法も説明します。 重要な考慮事項をいくつか紹介します

前提条件

必ずテスト広告でテストする

次のサンプルコードには、広告ユニット ID が含まれています。この ID を使用して、 テスト広告。このオブジェクトは、特定のリソースではなく、テスト広告を返すように 安全に使用できます。

ただし、 独自の広告ユニットを作成し アプリで使用する ID。デバイスをテスト用として明示的に設定します デバイス 必要があります。

Android

ca-app-pub-3940256099942544/2247696110

iOS

ca-app-pub-3940256099942544/3986624511

ネイティブ オーバーレイ広告を読み込む

ネイティブ オーバーレイ広告を読み込むには、Load() NativeOverlayAd クラス。読み込まれた NativeOverlayAd オブジェクトは、 完了ハンドラのパラメータ。

次のコードは、NativeOverlayAd を使用して広告を読み込みます。



  // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
  private string _adUnitId = "ca-app-pub-3940256099942544/2247696110";
#elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-3940256099942544/3986624511";
#else
  private string _adUnitId = "unused";
#endif


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

次のステップ