原生重疊廣告
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
開始使用
系統會透過平台原生 UI 元件,向使用者顯示原生重疊廣告。這類廣告會以疊加形式顯示在應用程式頂端。這與橫幅廣告的運作方式類似,但可自訂廣告外觀。
原生重疊廣告支援中介服務和影片廣告。這是原生重疊廣告相較於原生廣告的主要優勢。
本指南說明如何在 Unity 應用程式中導入原生重疊廣告,以及過程中需要注意的重要事項。
先決條件
- 完整閱讀入門指南。
- Unity 外掛程式 9.0.0 以上版本。
請一律使用測試廣告進行測試
下列程式碼範例包含廣告單元 ID,可用於要求測試廣告。這類 ID 經過特別設定,可針對每項要求傳回測試廣告,而非實際廣告,因此可安心使用。
不過,在 Ad Manager 網頁介面註冊應用程式,並建立要在應用程式中使用的廣告單元 ID 後,請在開發期間明確將裝置設為測試裝置。
/21775744923/example/native
載入原生重疊廣告
如要載入原生重疊廣告,請使用 NativeOverlayAd
類別的靜態 Load()
方法。載入的 NativeOverlayAd
物件會以參數形式,提供給完成處理常式。
下列程式碼使用 NativeOverlayAd
載入廣告:
// This ad unit is configured to always serve test ads.
private string _adUnitId = "/21775744923/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;
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-02 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-02 (世界標準時間)。"],[[["\u003cp\u003eNative overlay ads are presented as overlays within the app, similar to banners, but with customizable appearances and support for mediation and video ads.\u003c/p\u003e\n"],["\u003cp\u003eImplementing native overlay ads in Unity requires the Google Mobile Ads Unity plugin 9.0.0 or higher and completion of the Get Started guide.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can load, render with custom styles, and control the display (show/hide) of native overlay ads using the provided code examples and the \u003ccode\u003eNativeOverlayAd\u003c/code\u003e class.\u003c/p\u003e\n"],["\u003cp\u003eAlways test with test ads during development and remember to destroy the ad object when finished to release resources.\u003c/p\u003e\n"]]],["Native overlay ads are implemented in Unity using the `NativeOverlayAd` class. Key actions include loading ads with `NativeOverlayAd.Load()`, utilizing an ad request and optional native ad options. Rendering involves `RenderTemplate()`, allowing customization via `NativeTemplateStyle`. Ads can be shown using `Show()`, hidden with `Hide()`, and resources released by calling `Destroy()`. Testing is recommended using the provided test ad unit ID, and later configure actual ad units for development, and test devices.\n"],null,["Select platform: [Android](/ad-manager/mobile-ads-sdk/android/native \"View this page for the Android platform docs.\") [iOS](/ad-manager/mobile-ads-sdk/ios/native \"View this page for the iOS platform docs.\") [Flutter](/ad-manager/mobile-ads-sdk/flutter/native \"View this page for the Flutter platform docs.\") [Unity](/ad-manager/mobile-ads-sdk/unity/native-overlay \"View this page for the Unity platform docs.\")\n\n\u003cbr /\u003e\n\nGet started\n\nNative overlay ads are presented to users through UI components that are native\nto the platform. These ads are presented as an overlay on top of the\napplication. This is similar to how banner ads function, but with the ability to\ncustomize the appearance of the ads.\n\nNative overlay ads support mediation and video ads. This is a key advantage\nnative overlay ads have over [native ads](/admob/unity/native).\n\nThis guide shows you how to implement native overlay ads in a Unity app, as well\nas some important things to consider along the way.\n\nPrerequisites\n\n- Complete the [Get started guide](/ad-manager/mobile-ads-sdk/unity/quick-start).\n- Unity plugin 9.0.0 or higher.\n\nAlways test with test ads\n\nThe following sample code contains an ad unit ID which you can use to request\ntest ads. It's been specially configured to return test ads rather than\nproduction ads for every request, making it safe to use.\n\nHowever, after you've registered an app in the\nAd Manager web interface and created your own ad unit\nIDs for use in your app, explicitly [configure your device as a test\ndevice](/ad-manager/mobile-ads-sdk/unity/test-ads#enable_test_devices) during\ndevelopment.\n\n`/21775744923/example/native`\n\nLoad the native overlay ad\n\nLoading a native overlay ad is accomplished using the static `Load()` method on\nthe `NativeOverlayAd` class. The loaded `NativeOverlayAd` object is provided as\na parameter in the completion handler.\n\nThe following code uses `NativeOverlayAd` to load an ad: \n\n\n\n\n // This ad unit is configured to always serve test ads.\n private string _adUnitId = \"/21775744923/example/native\";\n\n\n\n private NativeOverlayAd _nativeOverlayAd;\n\n /// \u003csummary\u003e\n /// Loads the ad.\n /// \u003c/summary\u003e\n public void LoadAd()\n {\n // Clean up the old ad before loading a new one.\n if (_nativeOverlayAd != null)\n {\n DestroyAd();\n }\n\n Debug.Log(\"Loading native overlay ad.\");\n\n // Create a request used to load the ad.\n var adRequest = new AdRequest();\n\n // Optional: Define native ad options.\n var options = new NativeAdOptions\n {\n AdChoicesPosition = AdChoicesPlacement.TopRightCorner,\n MediaAspectRatio = NativeMediaAspectRatio.Any,\n };\n\n // Send the request to load the ad.\n NativeOverlayAd.Load(_adUnitId, adRequest, options,\n (NativeOverlayAd ad, LoadAdError error) =\u003e\n {\n if (error != null)\n {\n Debug.LogError(\"Native Overlay ad failed to load an ad \" +\n \" with error: \" + error);\n return;\n }\n\n // The ad should always be non-null if the error is null, but\n // double-check to avoid a crash.\n if (ad == null)\n {\n Debug.LogError(\"Unexpected error: Native Overlay ad load event \" +\n \" fired with null ad and null error.\");\n return;\n }\n\n // The operation completed successfully.\n Debug.Log(\"Native Overlay ad loaded with response : \" +\n ad.GetResponseInfo());\n _nativeOverlayAd = ad;\n\n // Register to ad events to extend functionality.\n RegisterEventHandlers(ad);\n });\n }\n\nRender and style the native overlay ad\n\nNative overlay ads are rendered using a `NativeTemplateStyle`. This class\ndefines fields that enable you to customize the ad's appearance.\n\nThe `TemplateID` is a required string which defines the native template used to\nrender the native overlay ad. Use the `NativeTemplateID` constant to choose an\nappropriate native template for your ad.\n\nThe following code renders the native overlay ad with a medium template and a\ncustom style. \n\n /// \u003csummary\u003e\n /// Renders the ad.\n /// \u003c/summary\u003e\n public void RenderAd()\n {\n if (_nativeOverlayAd != null)\n {\n Debug.Log(\"Rendering Native Overlay ad.\");\n\n // Define a native template style with a custom style.\n var style = new NativeTemplateStyle\n {\n TemplateID = NativeTemplateID.Medium,\n MainBackgroundColor = Color.red,\n CallToActionText = new NativeTemplateTextStyles\n {\n BackgroundColor = Color.green,\n FontColor = Color.white,\n FontSize = 9,\n Style = NativeTemplateFontStyle.Bold\n }\n };\n\n // Renders a native overlay ad at the default size\n // and anchored to the bottom of the screne.\n _nativeOverlayAd.RenderTemplate(style, AdPosition.Bottom);\n }\n }\n\nShow and hide the native overlay ad\n\nThe following code demonstrates how to show a loaded native overlay ad. \n\n /// \u003csummary\u003e\n /// Shows the ad.\n /// \u003c/summary\u003e\n public void ShowAd()\n {\n if (_nativeOverlayAd != null)\n {\n Debug.Log(\"Showing Native Overlay ad.\");\n _nativeOverlayAd.Show();\n }\n }\n\nHide the native overlay ad\n\nThe following code demonstrates how to hide a native overlay ad. \n\n /// \u003csummary\u003e\n /// Hides the ad.\n /// \u003c/summary\u003e\n public void HideAd()\n {\n if (_nativeOverlayAd != null)\n {\n Debug.Log(\"Hiding Native Overlay ad.\");\n _nativeOverlayAd.Hide();\n }\n }\n\nDestroy the native overlay ad\n\nWhen finished using the native overlay ad, be sure to call `Destroy()` to\nrelease resources. \n\n /// \u003csummary\u003e\n /// Destroys the native overlay ad.\n /// \u003c/summary\u003e\n public void DestroyAd()\n {\n if (_nativeOverlayAd != null)\n {\n Debug.Log(\"Destroying native overlay ad.\");\n _nativeOverlayAd.Destroy();\n _nativeOverlayAd = null;\n }\n }"]]