插頁式廣告會全螢幕顯示,覆蓋整個應用程式的介面。這類廣告通常會在應用程式流程中的自然轉換點顯示,例如在遊戲關卡之間的暫停時間。應用程式顯示插頁式廣告時,使用者可選擇輕觸廣告前往到達網頁,或關閉廣告並返回應用程式。
本指南說明如何將插頁式廣告整合至 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 範例:所有廣告格式的導入基本方法。