橫幅檢視區塊會占據畫面上的特定位置,並放送矩形圖片/文字廣告。使用者操作應用程式時,此類檢視區塊會持續停留在畫面上,並能定時自動重新整理內容。對剛接觸行動廣告的新手來說,這是很好的入門選擇。
本指南說明如何在 Unity 應用程式中整合橫幅廣告檢視區塊,內容涵蓋程式碼片段、操作說明、妥善調整橫幅廣告大小的方法,並提供其他資源連結。
先決條件
- 完整閱讀入門指南。
請一律使用測試廣告進行測試
下列程式碼範例包含廣告單元 ID,可用於要求測試廣告。這類 ID 經過特別設定,可針對每項要求傳回測試廣告,而非實際廣告,因此可安心使用。
不過,在 Ad Manager 網頁介面註冊應用程式,並建立要在應用程式中使用的廣告單元 ID 後,請在開發期間明確將裝置設為測試裝置。
/21775744923/example/adaptive-banner
初始化 Mobile Ads SDK
應用程式必須先呼叫 MobileAds.Initialize()
,初始化 Mobile Ads SDK 之後,才能載入廣告。這項操作只需執行一次,且最好在應用程式啟動時執行。
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
}
如果您使用中介服務,請等到回呼發生後再載入廣告,確保所有中介服務轉接程式都已初始化。
BannerView 範例
以下程式碼範例詳細說明了如何運用橫幅廣告檢視區塊。在本範例中,您會建立橫幅廣告檢視區塊例項,使用 AdManagerAdRequest
在檢視區塊中載入廣告,然後處理生命週期事件來擴充檢視區塊功能。
建立橫幅廣告檢視區塊
如要使用此類檢視區塊,首先須在附加到 GameObject
的 C# 指令碼中,建立橫幅廣告檢視區塊例項。
// This ad unit is configured to always serve test ads.
private string _adUnitId = "/21775744923/example/adaptive-banner";
AdManagerBannerView _bannerView;
/// <summary>
/// Creates a 320x50 banner view at top of the screen.
/// </summary>
public void CreateBannerView()
{
Debug.Log("Creating banner view");
// If we already have a banner, destroy the old one.
if (_bannerView != null)
{
DestroyAd();
}
// Create a 320x50 banner at top of the screen
_bannerView = new AdManagerBannerView(_adUnitId, AdSize.Banner, AdPosition.Top);
}
AdManagerBannerView
的建構函式包含以下參數:
adUnitId
:AdManagerBannerView
應從哪個廣告單元 ID 載入廣告。AdSize
:要使用的廣告大小。詳情請參閱「橫幅廣告大小」。AdPosition
:應放置橫幅廣告檢視區塊的位置。AdPosition
列舉會列出有效的廣告位置值。
請注意,您需要根據平台選用不同廣告單元。舉例來說,在 iOS 上提出廣告請求時,須使用 iOS 廣告單元;在 Android 上則為 Android 廣告單元。
(選用) 建立自訂位置的橫幅廣告檢視區塊
如要用比 AdPosition
值更精確的方式,控管 AdManagerBannerView
在畫面上的顯示位置,請使用包含 X 和 Y 座標參數的建構函式:
// Create a 320x50 banner views at coordinate (0,50) on screen.
_bannerView = new AdManagerBannerView(_adUnitId, AdSize.Banner, 0, 50);
系統會以畫面左上角為原點,根據傳入建構函式的 X 與 Y 值,定位 AdManagerBannerView
左上角的位置。
(選用) 建立自訂大小的橫幅廣告檢視區塊
除了使用 AdSize
常數外,您也可以自行指定廣告大小:
// Use the AdSize argument to set a custom size for the ad.
AdSize adSize = new AdSize(250, 250);
_bannerView = new AdManagerBannerView(_adUnitId, adSize, AdPosition.Bottom);
(選用) 多種廣告大小
您可以在 Ad Manager 中,指定多種可在 AdManagerBannerView
中放送的廣告大小。在 SDK 中實作這項功能前,請先建立委刊項,並將指定目標設為與不同大小廣告素材相關聯的相同廣告單元。
在應用程式中,將多項 AdSize
參數傳遞至 ValidAdSizes
:
var adView = new AdManagerBannerView(_adUnitId, AdSize.Banner, AdPosition.Top);
adView.ValidAdSizes = new List<AdSize>
{
AdSize.Banner, new AdSize(120, 20), new AdSize(250, 250),
};
如果 AdManagerAdView
在重新整理內容時變更大小,版面配置應能自動配合調整。AdManagerAdView
預設採用第一個參數傳入的尺寸,直到系統傳回下一個廣告為止。
載入橫幅廣告
AdManagerBannerView
就位後,請繼續使用 AdManagerBannerView
類別的 LoadAd()
方法載入廣告。該方法會用到 參數,其中包含執行階段資訊,例如指定目標資訊、排除標籤和發布商提供的 ID。
/// <summary>
/// Creates the banner view and loads a banner ad.
/// </summary>
public void LoadAd()
{
// create an instance of a banner view first.
if(_bannerView == null)
{
CreateAdManagerBannerView();
}
// create our request used to load the ad.
var adRequest = new AdManagerAdRequest();
// send the request to load the ad.
Debug.Log("Loading banner ad.");
_bannerView.LoadAd(adRequest);
}
監聽橫幅廣告檢視區塊事件
如要自訂廣告行為,您可以連結廣告生命週期中的多個事件,例如載入、開啟或關閉。如要監聽這些事件,請註冊委派項目:
/// <summary>
/// listen to events the banner view may raise.
/// </summary>
private void ListenToAdEvents()
{
// Raised when an ad is loaded into the banner view.
_bannerView.OnBannerAdLoaded += () =>
{
Debug.Log("Banner view loaded an ad with response : "
+ _bannerView.GetResponseInfo());
};
// Raised when an ad fails to load into the banner view.
_bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
{
Debug.LogError("Banner view failed to load an ad with error : "
+ error);
};
// Raised when the ad is estimated to have earned money.
_bannerView.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Banner view paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
_bannerView.OnAdImpressionRecorded += () =>
{
Debug.Log("Banner view recorded an impression.");
};
// Raised when a click is recorded for an ad.
_bannerView.OnAdClicked += () =>
{
Debug.Log("Banner view was clicked.");
};
// Raised when an ad opened full screen content.
_bannerView.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Banner view full screen content opened.");
};
// Raised when the ad closed full screen content.
_bannerView.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Banner view full screen content closed.");
};
}
刪除橫幅廣告檢視區塊
橫幅廣告檢視區塊使用完畢後,請務必呼叫 Destroy()
來釋出資源。
/// <summary>
/// Destroys the banner view.
/// </summary>
public void DestroyAd()
{
if (_bannerView != null)
{
Debug.Log("Destroying banner view.");
_bannerView.Destroy();
_bannerView = null;
}
}
大功告成!您的應用程式可以順利顯示橫幅廣告了。
重新整理廣告
如果您已啟用廣告單元的重新整理功能,就不必在廣告載入失敗時再次發送請求。Google Mobile Ads SDK 會採用您在 Ad Manager 使用者介面指定的重新整理頻率。如未啟用這項功能,則需重新發送請求。如要進一步瞭解廣告單元重新整理功能 (例如如何設定重新整理頻率),請參閱「行動應用程式中的廣告重新整理頻率」。
橫幅廣告大小
下表列出標準橫幅廣告的大小。
尺寸以 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
。這類事件可能在廣告生命週期的任意時間點發生,即使是在呼叫負載前也不例外。
namespace GoogleMobileAds.Api.AdManager;
/// The App event message sent from the ad.
public class AppEvent
{
// Name of the app event.
string Name;
// Argument passed from the app event.
string Value;
}
廣告中發生應用程式事件時,會觸發 OnAppEventReceived
。以下範例說明如何運用程式碼處理此類事件:
_bannerview.OnAppEventReceived += (AppEvent args) =>
{
Debug.Log($"Received app event from the ad: {args.Name}, {args.Value}.");
};
下方範例則演示如何依據名為顏色的應用程式事件,變更應用程式的背景顏色:
_bannerview.OnAppEventReceived += (AppEvent args) =>
{
if (args.Name == "color")
{
Color color;
if (ColorUtility.TryParseColor(arg.Value, out color))
{
gameObject.GetComponent<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: 0px;
left: 0px;
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 示例: 所有廣告格式最簡單的導入方式。