插頁式獎勵廣告是一種獎勵廣告格式,可在應用程式自然轉換時自動顯示廣告,並向使用者提供獎勵。與獎勵廣告不同的是,插頁式獎勵廣告不需要等使用者確定選擇觀看即可放送。
必要條件
- 完成入門指南。
一律使用測試廣告進行測試
下列程式碼範例包含廣告單元 ID,可用於要求測試廣告。這個版本經過特別設定,可針對每個請求傳回測試廣告,而非正式版廣告,因此可安心使用。
不過,在 AdMob 網頁介面中註冊應用程式,並建立要在應用程式中使用的廣告單元 ID 後,請在開發期間將裝置明確設為測試裝置。
Android
ca-app-pub-3940256099942544/5354046379
iOS
ca-app-pub-3940256099942544/6978759866
初始化 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.
});
}
}
如果您使用中介服務,請等到回呼發生後再載入廣告,確保所有中介服務轉接程式都已初始化。
導入作業
整合插頁式獎勵廣告的主要步驟如下:
- 載入插頁式獎勵廣告
- [選用] 驗證伺服器端驗證 (SSV) 回呼
- 顯示插頁式獎勵廣告並回呼獎勵
- 監聽插頁式獎勵廣告事件
- 清除插頁式獎勵廣告
- 預先載入下一則插頁式獎勵廣告
載入插頁式獎勵廣告
如要載入插頁式獎勵廣告,請使用 RewardedInterstitialAd
類別的靜態 Load()
方法。載入方法需要廣告單元 ID、AdRequest
物件,以及在廣告載入成功或失敗時呼叫的完成處理常式。載入的 RewardedInterstitialAd
物件會做為完成處理常式中的參數提供。以下範例說明如何載入 RewardedInterstitialAd
。
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/5354046379";
#elif UNITY_IPHONE
private string _adUnitId = "ca-app-pub-3940256099942544/6978759866";
#else
private string _adUnitId = "unused";
#endif
private RewardedInterstitialAd _rewardedInterstitialAd;
/// <summary>
/// Loads the rewarded interstitial ad.
/// </summary>
public void LoadRewardedInterstitialAd()
{
// Clean up the old ad before loading a new one.
if (_rewardedInterstitialAd != null)
{
_rewardedInterstitialAd.Destroy();
_rewardedInterstitialAd = null;
}
Debug.Log("Loading the rewarded interstitial ad.");
// create our request used to load the ad.
var adRequest = new AdRequest();
adRequest.Keywords.Add("unity-admob-sample");
// send the request to load the ad.
RewardedInterstitialAd.Load(_adUnitId, adRequest,
(RewardedInterstitialAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("rewarded interstitial ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Rewarded interstitial ad loaded with response : "
+ ad.GetResponseInfo());
_rewardedInterstitialAd = ad;
});
}
[選用] 驗證伺服器端驗證 (SSV) 回呼
如果應用程式需要在伺服器端驗證回呼中加入額外資料,請使用獎勵插頁式廣告的自訂資料功能。在已設定獎勵廣告物件的任何字串值,都會傳遞至 SSV 回呼的 custom_data
查詢參數。如果未設定任何自訂資料值,SSV 回呼就不會包含 custom_data
查詢參數值。
下列程式碼範例示範如何在載入有獎中介插頁式廣告後,設定 SSV 選項。
// send the request to load the ad.
RewardedInterstitialAd.Load(_adUnitId,
adRequest,
(RewardedInterstitialAd ad, LoadAdError error) =>
{
// If the operation failed, an error is returned.
if (error != null || ad == null)
{
Debug.LogError("Rewarded interstitial ad failed to load an ad " +
" with error : " + error);
return;
}
// If the operation completed successfully, no error is returned.
Debug.Log("Rewarded interstitial ad loaded with response : " +
ad.GetResponseInfo());
// Create and pass the SSV options to the rewarded ad.
var options = new ServerSideVerificationOptions
.Builder()
.SetCustomData("SAMPLE_CUSTOM_DATA_STRING")
.Build()
ad.SetServerSideVerificationOptions(options);
});
如要設定自訂獎勵字串,請務必在顯示廣告前完成設定。
顯示插頁式獎勵廣告並回呼獎勵
顯示廣告時,您必須提供回呼,以處理使用者的獎勵。每次載入只能顯示一次廣告。使用 CanShowAd()
方法驗證廣告是否已準備好放送。
下列程式碼是顯示插頁式獎勵廣告的最佳方法。
public void ShowRewardedInterstitialAd()
{
const string rewardMsg =
"Rewarded interstitial ad rewarded the user. Type: {0}, amount: {1}.";
if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
{
rewardedInterstitialAd.Show((Reward reward) =>
{
// TODO: Reward the user.
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
});
}
}
監聽插頁式獎勵廣告事件
如要進一步自訂廣告的行為,您可以連結廣告生命週期中的多個事件。如要監聽這些事件,請註冊委派項目,如下所示。
private void RegisterEventHandlers(RewardedInterstitialAd ad)
{
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Rewarded interstitial ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("Rewarded interstitial ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
ad.OnAdClicked += () =>
{
Debug.Log("Rewarded interstitial ad was clicked.");
};
// Raised when an ad opened full screen content.
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Rewarded interstitial ad full screen content opened.");
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Rewarded interstitial ad full screen content closed.");
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded interstitial ad failed to open " +
"full screen content with error : " + error);
};
}
清除插頁式獎勵廣告
使用完 RewardedInterstitialAd
後,請務必先呼叫 Destroy()
方法,再捨棄對該物件的參照:
_rewardedInterstitialAd.Destroy();
這會通知外掛程式該物件不再使用,因此可回收其占用的記憶體。如果未呼叫這個方法,就會導致記憶體流失。
預先載入下一則插頁式獎勵廣告
RewardedInterstitialAd
是只能使用一次的物件。也就是說,一旦顯示過中介插頁式獎勵廣告,該物件就無法再次使用。如要要求另一個插頁式獎勵廣告,請載入新的 RewardedInterstitialAd
物件。
如要為下一次曝光機會準備獎勵插頁式廣告,請在系統發出 OnAdFullScreenContentClosed
或 OnAdFullScreenContentFailed
廣告事件後,預先載入獎勵插頁式廣告。
private void RegisterReloadHandler(RewardedInterstitialAd ad)
{
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += ()
{
Debug.Log("Rewarded interstitial ad full screen content closed.");
// Reload the ad so that we can show another as soon as possible.
LoadRewardedInterstitialAd();
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded interstitial ad failed to open " +
"full screen content with error : " + error);
// Reload the ad so that we can show another as soon as possible.
LoadRewardedInterstitialAd();
};
}
其他資源
- HelloWorld 範例: 所有廣告格式的最簡單導入方式。