插頁式廣告是全螢幕廣告,蓋住代管應用程式的介面。這類廣告通常會在應用程式流程中的自然轉換點顯示,例如在遊戲關卡之間的暫停期間。應用程式顯示插頁式廣告時,使用者可選擇輕觸廣告並繼續前往其到達網頁,或是關閉廣告並返回應用程式。 個案研究。
本指南說明如何將插頁式廣告整合至 Unity 應用程式。
必要條件
- 完成入門指南。
Always test with test ads
The sample code below contains an ad unit ID which you can use to request test ads. It's been specially configured to return test ads rather than production ads for every request, making it safe to use.
However, after you've registered an app in the AdMob web interface and created your own ad unit IDs for use in your app, explicitly configure your device as a test device during development.
Android
ca-app-pub-3940256099942544/1033173712
iOS
ca-app-pub-3940256099942544/4411468910
初始化 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、AdRequest
物件,以及廣告載入成功或失敗時呼叫的完成處理常式。完成的 InterstitialAd
物件會在完成處理常式中做為參數提供。以下範例說明如何載入 InterstitialAd
。
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
private string _adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
private string _adUnitId = "unused";
#endif
private _interstitialAd;
/// <summary>
/// Loads the interstitial ad.
/// </summary>
public void LoadLoadInterstitialAd()
{
// 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 AdRequest();
// send the request to load the ad.
InterstitialAd.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;
});
}
顯示插頁式廣告
如要顯示已載入的插頁式廣告,請在 InterstitialAd
執行個體上呼叫 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);
};
}
清除插頁式廣告
使用完 InterstitialAd
後,請務必先呼叫 Destroy()
方法,再捨棄對該方法的參照:
_interstitialAd.Destroy();
這會通知外掛程式停止使用物件,並可收回物件所佔用的記憶體。如未呼叫此方法,就會造成記憶體流失。
預先載入下一個插頁式廣告
插頁式廣告是一次性的物件。也就是說,插頁式廣告顯示後,物件就無法再次使用。如要請求其他插頁式廣告,請建立新的 InterstitialAd
物件。
如要準備插頁式廣告供下一個曝光商機使用,請在 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();
};
}
最佳做法
- 判斷插頁式廣告是否為適合您應用程式的廣告類型。
- 插頁式廣告最適合包含自然轉換點的應用程式。 透過應用程式內的工作結束時 (例如分享圖片或完成遊戲關卡),即會產生這類點數。請務必考量應用程式的哪個時間點顯示最適當的多媒體廣告,以及使用者應如何回應。
- 顯示插頁式廣告時,請將動作暫停。
- 插頁式廣告有多種類型,例如文字、圖像或影片。請務必確認應用程式顯示插頁式廣告時,也會暫停使用部分資源,以便廣告充分利用。舉例來說,當您呼叫顯示插頁式廣告時,請務必暫停應用程式產生的所有音訊輸出。您可以在
OnAdFullScreenContentClosed()
事件中繼續播放音效,當使用者完成與廣告互動後,即可叫用該事件。此外,建議您在廣告顯示時暫停所有密集的運算工作 (例如遊戲迴圈)。這樣可確保使用者不會體驗緩慢或不回應的圖像,也不會觀看影片是否播放不流暢。 - 不要讓應用程式廣告氾濫。
- 增加應用程式內插頁式廣告的頻率似乎是增加收益的好方法,但也可能破壞使用者體驗並降低點閱率。請確保使用者不會經常打斷他們使用您的應用程式。
其他資源
- HelloWorld 範例:採取所有廣告格式的最少導入做法。