原生重疊廣告

開始使用

原生重疊廣告會透過原生的 UI 元件向使用者顯示 這個平台這類廣告會以重疊廣告形式顯示在 應用程式。這與橫幅廣告的運作方式類似,而且還能 自訂廣告外觀

原生重疊廣告支援中介服務和影片廣告。主要優勢 原生重疊廣告比原生廣告更多。

本指南將說明如何在 Unity 應用程式中導入原生重疊廣告 學習過程中需要考量的幾個重點

必要條件

  • 完成入門指南
  • Unity 外掛程式 9.0.0 以上版本。

一律使用測試廣告進行測試

以下程式碼範例包含廣告單元 ID,供您請求 測試廣告該功能已設定為傳回測試廣告,而不是 導入及導入廣告,以備不時之需。

不過,在 AdMob 網頁介面並建立自己的廣告單元 用於應用程式的 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;
    }
}

後續步驟