橫幅檢視區塊會占據螢幕上部分版面,放送矩形圖片/文字廣告。使用者操作應用程式時,此類檢視區塊會持續停留在畫面上,並能定時自動重新整理內容。對剛接觸行動廣告的新手來說,這是很好的入門選擇。
本指南說明如何在 Unity 應用程式中整合橫幅廣告檢視區塊,內容涵蓋程式碼片段、操作說明、妥善調整橫幅廣告大小的方法,並提供其他資源連結。
必備條件
- 完整閱讀入門指南。
請務必使用測試廣告進行測試
下列程式碼範例包含廣告單元 ID,可用於要求測試廣告。這類 ID 經過特別設定,可針對每項要求傳回測試廣告,而非實際廣告,因此可安心使用。
不過,在 Ad Manager 網頁介面註冊應用程式,並建立要在應用程式中使用的廣告單元 ID 後,請在開發期間明確將裝置設為測試裝置。
/21775744923/example/adaptive-banner
初始化 Mobile Ads SDK
應用程式必須先呼叫 MobileAds.Initialize()
,初始化 Google Mobile Ads SDK 之後,才能載入廣告。這項操作只需執行一次,最佳時機是應用程式啟動時。
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
}
如果使用中介服務,請等到回呼發生後再載入廣告,確保所有中介服務轉接程式都已初始化。
BannerView 範例
以下程式碼範例詳述如何使用橫幅廣告檢視區塊。在本例中,您會建立橫幅廣告檢視區塊例項,使用 AdManagerAdRequest
在橫幅廣告檢視區塊中載入廣告,然後處理生命週期事件來擴充檢視區塊功能。
建立橫幅廣告檢視區塊
使用橫幅廣告檢視區塊的第一步,是建立橫幅廣告檢視區塊的例項。
將 AD_UNIT_ID 替換為廣告單元 ID。
AdManagerBannerView
的建構函式包含以下參數:
adUnitId
:要載入的橫幅廣告單元 ID。AdSize
:要使用的橫幅廣告大小。AdPosition
:應放置橫幅廣告檢視區塊的位置。
(選用) 建立自訂位置的橫幅廣告檢視區塊
如要用比 AdPosition
值更精確的方式,控管橫幅廣告檢視區塊在螢幕上的顯示位置,請使用包含 X 和 Y 座標參數的建構函式:
系統會以畫面左上角為原點,根據傳入建構函式的 X 與 Y 值,定位橫幅廣告檢視區塊左上角的位置。
(選用) 建立自訂大小的橫幅廣告檢視區塊
除了使用 AdSize
常數,您也可以自行指定廣告大小:
(選用) 多種廣告大小
您可以在 Ad Manager 中,指定多種可在 AdManagerBannerView
中放送的廣告大小。在 SDK 中實作這項功能前,請先建立委刊項,並將指定目標設為與不同大小廣告素材相關聯的相同廣告單元。
在應用程式中,將多項 AdSize
參數傳遞至 ValidAdSizes
:
如果 AdManagerAdView
的大小在重新整理內容時變更,版面配置應自動配合調整至新的大小。AdManagerAdView
預設採用第一個參數傳入的尺寸,直到系統傳回下一個廣告為止。
載入橫幅廣告
AdManagerBannerView
就位後,請繼續使用 AdManagerBannerView
類別的 LoadAd()
方法載入廣告。該方法會用到 參數,其中包含執行階段資訊,例如指定目標資訊、排除標籤和發布商提供的 ID。
如要載入廣告,請建立 AdManagerAdRequest
並傳遞至 LoadAd()
方法。
// Send a request to load an ad into the banner view.
adManagerBannerView.LoadAd(new AdManagerAdRequest());
監聽橫幅廣告檢視事件
如要自訂廣告行為,您可以連結廣告生命週期中的多個事件,例如載入、開啟或關閉。如要監聽這些事件,請註冊委派項目:
adManagerBannerView.OnBannerAdLoaded += () =>
{
// Raised when an ad is loaded into the banner view.
};
adManagerBannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
{
// Raised when an ad fails to load into the banner view.
};
adManagerBannerView.OnAdPaid += (AdValue adValue) =>
{
// Raised when the ad is estimated to have earned money.
};
adManagerBannerView.OnAdImpressionRecorded += () =>
{
// Raised when an impression is recorded for an ad.
};
adManagerBannerView.OnAdClicked += () =>
{
// Raised when a click is recorded for an ad.
};
adManagerBannerView.OnAdFullScreenContentOpened += () =>
{
// Raised when an ad opened full screen content.
};
adManagerBannerView.OnAdFullScreenContentClosed += () =>
{
// Raised when the ad closed full screen content.
};
刪除橫幅廣告檢視區塊
橫幅廣告檢視區塊使用完畢後,請務必呼叫 Destroy()
以釋出資源。
if (adManagerBannerView != null)
{
// Always destroy the banner view when no longer needed.
adManagerBannerView.Destroy();
adManagerBannerView = null;
}
大功告成!應用程式可以顯示橫幅廣告了。
重新整理廣告
如已設定廣告單元重新整理功能,廣告載入失敗時,就不需要請求其他廣告。Google Mobile Ads SDK 會套用您在 Ad Manager UI 指定的重新整理頻率。如果您未啟用這項功能,請發出新請求。如要進一步瞭解廣告單元的重新整理功能,例如怎麼設定重新整理頻率,請參閱「行動應用程式中廣告的重新整理頻率」。
橫幅廣告大小
下表列出標準橫幅廣告大小。
尺寸以 dp 為單位 (寬 x 高) | 說明 | 適用裝置 | AdSize 常數 |
---|---|---|---|
320x50 | 標準橫幅廣告 | 手機和平板電腦 | BANNER |
320x100 | 大型橫幅廣告 | 手機和平板電腦 | LARGE_BANNER |
300x250 | IAB 中矩形廣告 | 手機和平板電腦 | MEDIUM_RECTANGLE |
468x60 | IAB 完整橫幅廣告 | 平板電腦 | FULL_BANNER |
728x90 | IAB 超級橫幅廣告 | 平板電腦 | LEADERBOARD |
提供的寬度 x 自動調整高度 | 自動調整橫幅廣告 | 手機和平板電腦 | 不適用 |
螢幕寬度 x 32|50|90 | 智慧型橫幅廣告 | 手機和平板電腦 | SMART_BANNER |
進一步瞭解即將取代智慧型橫幅廣告的自動調整橫幅廣告。 |
應用程式事件
您可以利用應用程式事件,在建立廣告時加入向應用程式碼傳送訊息的功能,讓應用程式在收到訊息後採取適當動作。
如要監聽 Ad Manager 專屬的應用程式事件,請使用 AppEvent
。這類事件可能在廣告生命週期的任意時間點發生,即使是在呼叫負載前也不例外。
廣告中發生應用程式事件時,會觸發 OnAppEventReceived
。以下範例說明如何運用程式碼處理此類事件:
adManagerBannerView.OnAppEventReceived += (AppEvent args) =>
{
Debug.Log($"Received app event from the ad: {args.Name}, {args.Data}.");
};
下方範例則演示如何依據名為顏色的應用程式事件,變更應用程式的背景顏色:
adManagerBannerView.OnAppEventReceived += (AppEvent args) =>
{
if (args.Name == "color")
{
Color color;
if (ColorUtility.TryParseHtmlString(args.Data, out color))
{
renderer.material.color = color;
}
}
};
此外,以下是傳送顏色應用程式事件時的相應廣告素材:
<html>
<head>
<script src="//www.gstatic.com/afma/api/v1/google_mobile_app_ads.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
// Send a color=green event when ad loads.
admob.events.dispatchAppEvent("color", "green");
document.getElementById("ad").addEventListener("click", function() {
// Send a color=blue event when ad is clicked.
admob.events.dispatchAppEvent("color", "blue");
});
});
</script>
<style>
#ad {
width: 320px;
height: 50px;
top: 0;
left: 0;
font-size: 24pt;
font-weight: bold;
position: absolute;
background: black;
color: white;
text-align: center;
}
</style>
</head>
<body>
<div id="ad">Carpe diem!</div>
</body>
</html>
其他資源
- HelloWorld 範例: 所有廣告格式的最簡單導入方式。