插頁式廣告是全螢幕廣告,會覆蓋主應用程式的介面。這類廣告通常會顯示在應用程式流程中的自然轉換點,例如遊戲關卡之間的暫停期間。應用程式顯示插頁式廣告時,使用者可以選擇輕觸廣告前往到達網頁,或是關閉廣告返回應用程式。
本指南說明如何將插頁式廣告整合至 Unity 應用程式。
先備知識
- 完成入門指南。
一律使用測試廣告進行測試
以下程式碼範例包含可用於請求測試廣告的廣告單元 ID。其經過特別設定,為每個請求傳回測試廣告,而非製作實際廣告,因此可以安全使用。
不過,您已在Ad Manager 網頁介面中註冊應用程式,並建立自己的廣告單元 ID 以用於應用程式,請在開發期間明確將裝置設定為測試裝置。
/6499/example/interstitial
初始化 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.
});
}
}
如果您使用中介服務,請等到回呼執行後再載入廣告,這麼做可確保所有中介服務轉接程式都已初始化。
導入作業
整合插頁式廣告的主要步驟如下:
- 載入插頁式廣告
- 顯示插頁式廣告
- 監聽插頁式廣告事件
- 清除插頁式廣告
- 預先載入下一個插頁式廣告
載入插頁式廣告
您可以在 InterstitialAd
類別上使用靜態 Load()
方法載入插頁式廣告。載入方法需要廣告單元 ID、AdManagerAdRequest
物件,以及廣告載入成功或失敗時呼叫的完成處理常式。系統會在完成處理常式中以參數的形式提供載入的 AdManagerInterstitialAd
物件。以下範例說明如何載入 AdManagerInterstitialAd
。
// This ad unit is configured to always serve test ads.
private string _adUnitId = "/6499/example/interstitial";
private InterstitialAd _interstitialAd;
/// <summary>
/// Loads the interstitial ad.
/// </summary>
public void LoadInterstitialAd()
{
// Clean up the old ad before loading a new one.
if (_interstitialAd != null)
{
_interstitialAd.Destroy();
_interstitialAd = null;
}
Debug.Log("Loading the interstitial ad.");
// create our request used to load the ad.
var adRequest = new AdManagerAdRequest();
// send the request to load the ad.
AdManagerInterstitialAd.Load(_adUnitId, adRequest,
(InterstitialAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("interstitial ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Interstitial ad loaded with response : "
+ ad.GetResponseInfo());
_interstitialAd = ad;
});
}
顯示插頁式廣告
如要顯示已載入的插頁式廣告,請在 AdManagerInterstitialAd
例項上呼叫 Show()
方法。廣告每次載入時可能會顯示一次。請使用 CanShowAd()
方法,確認廣告已經準備好放送。
/// <summary>
/// Shows the interstitial ad.
/// </summary>
public void ShowInterstitialAd()
{
if (_interstitialAd != null && _interstitialAd.CanShowAd())
{
Debug.Log("Showing interstitial ad.");
_interstitialAd.Show();
}
else
{
Debug.LogError("Interstitial ad is not ready yet.");
}
}
監聽插頁式廣告事件
如要進一步自訂廣告的行為,您可以在廣告生命週期中觸發多項事件。註冊委派即可監聽這些事件,如下所示。
private void RegisterEventHandlers(InterstitialAd interstitialAd)
{
// Raised when the ad is estimated to have earned money.
interstitialAd.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Interstitial ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
interstitialAd.OnAdImpressionRecorded += () =>
{
Debug.Log("Interstitial ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
interstitialAd.OnAdClicked += () =>
{
Debug.Log("Interstitial ad was clicked.");
};
// Raised when an ad opened full screen content.
interstitialAd.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Interstitial ad full screen content opened.");
};
// Raised when the ad closed full screen content.
interstitialAd.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Interstitial ad full screen content closed.");
};
// Raised when the ad failed to open full screen content.
interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
};
}
清除插頁式廣告
完成 AdManagerInterstitialAd
後,請務必先呼叫 Destroy()
方法,再放置對該方法的參照:
_interstitialAd.Destroy();
這會通知外掛程式,該物件已不再使用,且可以收回該物件佔用的記憶體。無法呼叫此方法會導致記憶體流失。
預先載入下一個插頁式廣告
插頁式廣告是一次性的物件。也就是說,顯示插頁式廣告後,就無法再使用該物件。如要請求其他插頁式廣告,請建立新的 AdManagerInterstitialAd
物件。
如要為下一個曝光商機做好準備,請在觸發 OnAdFullScreenContentClosed
或 OnAdFullScreenContentFailed
廣告事件後預先載入插頁式廣告。
private void RegisterReloadHandler(InterstitialAd interstitialAd)
{
// Raised when the ad closed full screen content.
interstitialAd.OnAdFullScreenContentClosed += ()
{
Debug.Log("Interstitial Ad full screen content closed.");
// Reload the ad so that we can show another as soon as possible.
LoadInterstitialAd();
};
// Raised when the ad failed to open full screen content.
interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
// Reload the ad so that we can show another as soon as possible.
LoadInterstitialAd();
};
}
應用程式事件
使用應用程式事件,您就能建立可傳送訊息至應用程式程式碼的廣告。然後,應用程式即可根據這些訊息採取行動。
您可以使用 AppEvent
監聽 Ad Manager 專屬應用程式事件。這些事件可能會在廣告生命週期中隨時發生,即使在呼叫載入前也是如此。
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
。以下範例說明如何在程式碼中處理這個事件:
_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
Debug.Log($"Received app event from the ad: {args.Name}, {args.Value}.");
};
以下範例說明如何根據含有顏色名稱的應用程式事件,變更應用程式背景顏色:
_interstitialAd.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>
最佳做法
- 判斷插頁式廣告是否適合您應用程式的廣告類型。
- 插頁式廣告最適合包含自然轉換點的應用程式。 應用程式內的工作結束時 (例如分享圖片或完成遊戲關卡),即會產生此點。請務必考量到應用程式中放送插頁式廣告的最佳時機,以及使用者的可能反應。
- 在顯示插頁式廣告時暫停動作。
- 插頁式廣告有多種類型,例如文字、圖像或影片。請務必確保應用程式顯示插頁式廣告時,也會暫停使用部分資源,以便廣告加以利用。舉例來說,當您呼叫以顯示插頁式廣告時,請務必暫停應用程式產生的任何音訊輸出。您可以在
OnAdFullScreenContentClosed()
事件中繼續播放音訊,在使用者完成與廣告互動後即可叫用。此外,建議您在顯示廣告時,暫時停止任何密集的運算工作,例如遊戲迴圈。以確保使用者不會體驗緩慢或不回應的圖像,或影片發生延遲。 - 不要讓應用程式廣告氾濫。
- 提高應用程式內插頁式廣告的展示頻率似乎是增加收益的好方法,但也可能破壞使用者體驗並降低點閱率。請確保使用者不會經常受到干擾,因此無法再使用您的應用程式。
其他資源
- HelloWorld 範例:最簡單導入所有廣告格式。