上次更新時間:2023 年 2 月
本頁面涵蓋目前和先前版本的遷移作業。
從第 7 版遷移至第 8 版
全螢幕格式現在使用靜態載入方法
在第 7 版外掛程式中,插頁式和獎勵廣告有例項層級的 LoadAd() 方法可載入廣告,而插頁式獎勵廣告和應用程式開啟頁面廣告則有靜態 Load() 方法可載入廣告。在第 8 版中,所有全螢幕廣告格式 (插頁式、獎勵、插頁式獎勵廣告和應用程式開啟頁面廣告) 都會使用靜態 Load() 方法載入廣告。以下是載入插頁式廣告的範例:
版本 8 (目前)
#if UNITY_ANDROID
const string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
const string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
const string adUnitId = "unexpected_platform";
#endif
private InterstitialAd _interstitialAd;
private void LoadAd()
{
// Load an interstitial ad
InterstitialAd.Load(adUnitId, new AdRequest(),
(InterstitialAd ad, LoadAdError loadAdError) =>
{
if (loadAdError != null)
{
Debug.Log("Interstitial ad failed to load with error: " +
loadAdError.GetMessage());
return;
}
else if (ad == null)
{
Debug.Log("Interstitial ad failed to load.");
return;
}
Debug.Log("Interstitial ad loaded.");
_interstitialAd = ad;
});
}
第 7 版 (舊版)
#if UNITY_ANDROID
const string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
const string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
const string adUnitId = "unexpected_platform";
#endif
private InterstitialAd _interstitialAd;
private void LoadInterstitialAd()
{
// Initialize an InterstitialAd.
_interstitialAd = new InterstitialAd(adUnitId);
// Called when an ad request has successfully loaded.
_interstitialAd.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request has failed to load.
_interstitialAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
_interstitialAd.LoadAd(request);
}
private void HandleOnAdLoaded(object sender, EventArgs args)
{
Debug.Log("Interstitial ad loaded.");
}
private void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
if (args != null)
{
Debug.Log("Interstitial ad failed to load with error: " +
args.LoadAdError.GetMessage());
}
}
以下是載入獎勵廣告的範例:
版本 8 (目前)
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
const string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
const string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
const string adUnitId = "unused";
#endif
private RewardedAd _rewardedAd;
private void LoadRewardedAd()
{
// Load a rewarded ad
RewardedAd.Load(adUnitId, new AdRequest(),
(Rewarded ad, LoadAdError loadError) =>
{
if (loadError != null)
{
Debug.Log("Rewarded ad failed to load with error: " +
loadError.GetMessage());
return;
}
else if (ad == null)
{
Debug.Log("Rewarded ad failed to load.");
return;
}
Debug.Log("Rewarded ad loaded.");
_rewardedAd = ad;
});
}
第 7 版 (舊版)
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
const string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
const string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
const string adUnitId = "unused";
#endif
private RewardedAd _rewardedAd;
private void LoadRewardedAd()
{
// Initialize an InterstitialAd.
_rewardedAd = new RewardedAd(adUnitId);
// Called when an ad request has successfully loaded.
_rewardedAd.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request has failed to load.
_rewardedAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
_rewardedAd.LoadAd(request);
}
private void HandleOnAdLoaded(object sender, EventArgs args)
{
Debug.Log("Rewarded ad loaded.");
}
private void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
if (args != null)
{
Debug.Log("Rewarded ad failed to load with error: " +
args.LoadAdError.GetMessage());
}
}
使用 CanShowAd() 檢查是否可放送全螢幕廣告
在第 7 版中,全螢幕廣告 (插頁式、獎勵、插頁式獎勵和應用程式開啟頁面廣告) 具有 IsLoaded() 方法,如果廣告已載入,則會傳回 true。由於廣告載入方式有所變更,在第 8 版中,您必須等到廣告載入後,才能存取全螢幕廣告物件,因此 IsLoaded() 方法已淘汰。
第 8 版新增了 CanShowAd() 方法,如果廣告仍可顯示,則會傳回 true。以下舉例說明如何在插頁式廣告中使用 CanShowAd():
版本 8 (目前)
private InterstitialAd _interstitalAd;
public void ShowInterstitialAd()
{
if (_interstitalAd != null && _interstitalAd.CanShowAd())
{
_interstitalAd.Show();
}
else
{
Debug.Log("Interstitial ad cannot be shown.");
}
}
第 7 版 (舊版)
private InterstitialAd _interstitalAd;
public void ShowInterstitialAd()
{
if (_interstitalAd != null && _interstitalAd.IsLoaded())
{
_interstitalAd.Show();
}
else
{
Debug.Log("Interstitial ad is not ready yet.");
}
}
使用 Show(Action) 顯示獎勵廣告
在外掛程式第 7 版中,獎勵廣告有 Show() 方法,並有獨立的 OnUserEarnedReward 事件來處理使用者獎勵信號;插頁式獎勵廣告則有 Show(Action<Reward>) 方法,並有回呼來處理使用者獎勵信號。在第 8 版中,獎勵和插頁式獎勵廣告格式將提供 Show(Action<Reward>) 方法,並附帶回呼,用於處理使用者獎勵通知。
以下是顯示獎勵廣告的範例:
版本 8 (目前)
private RewardedAd _rewardedAd;
public void ShowRewardedAd()
{
if (_rewardedAd != null && _rewardedAd.CanShowAd())
{
_rewardedAd.Show((Reward reward) =>
{
Debug.Log("Rewarded ad granted a reward: " +
reward.Amount);
});
}
else
{
Debug.Log("Rewarded ad cannot be shown.");
}
}
第 7 版 (舊版)
private RewardedAd _rewardedAd;
public void ShowRewardedAd()
{
if (_rewardedAd != null && _rewardedAd.CanShowAd())
{
_rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
_rewardedAd.Show();
}
else
{
Debug.Log("Rewarded ad is not ready yet.");
}
}
public void HandleUserEarnedReward(object sender, Reward reward)
{
Debug.Log("Rewarded ad granted a reward: " +
reward.Amount);
}
廣告事件委派現在採用特定類型引數
在第 7 版 API 中,我們在定義事件委派時使用了 EventHandlers。在第 8 版中,我們採用一般委派來處理廣告事件。因此,事件現在會直接發出事件值,而不會包裝在 EventArg 類別中。
以下是使用 OnAdPaid (取代 OnPaidEvent) 的範例:
版本 8 (目前)
private BannerView _bannerView;
public void ConfigureBanner()
{
_bannerView.OnAdPaid += (AdValue value) =>
{
AdValue value = value;
};
}
第 7 版 (舊版)
private BannerView _bannerView;
public void ConfigureBanner()
{
_bannerView.OnPaidEvent += (object sender, AdValueEventArg arg) =>
{
AdValue value = arg.Value;
};
}
廣告格式現在符合統一介面
在外掛程式第 7 版中,全螢幕廣告格式的事件名稱不一致。在第 8 版中,我們重新命名了許多 API 方法,讓各種廣告格式保持一致。
下表列出第 8 版中推出的類別異動。
BannerView | |
|---|---|
| 第 7 版 | v8 |
| OnAdLoaded | OnBannerAdLoaded |
| OnAdFailedToLoad | OnBannerAdLoadFailed |
| OnAdOpening | OnAdFullScreenContentOpened |
| OnAdClosed | OnAdFullScreenContentClosed |
| OnPaidEvent | OnAdPaid |
InterstitialAd | |
| LoadAd() | InterstitialAd.Load() |
| InterstitialAd() | InterstitialAd.Load() |
| OnAdLoaded | InterstitialAd.Load() |
| OnAdFailedToLoad | InterstitialAd.Load() |
| OnAdOpening | OnAdFullScreenContentOpened |
| OnAdClosed | OnAdFullScreenContentClosed |
| OnAdFailedToShow | OnAdFullScreenContentFailed |
| OnAdDidRecordImpression | OnAdImpressionRecorded |
| OnPaidEvent | OnAdPaid |
RewardedAd | |
| LoadAd() | RewardedAd.Load() |
| RewardedAd() | RewardedAd.Load() |
| OnAdLoaded | RewardedAd.Load() |
| OnAdFailedToLoad | RewardedAd.Load() |
| OnAdOpening | OnAdFullScreenContentOpened |
| OnAdClosed | OnAdFullScreenContentClosed |
| OnAdFailedToShow | OnAdFullScreenContentFailed |
| OnAdDidRecordImpression | OnAdImpressionRecorded |
| OnPaidEvent | OnAdPaid |
| Show() | Show() |
| OnUserEarnedReward | Show() |
RewardedInterstitialAd | |
| LoadAd() | RewardedInterstitialAd.Load() |
| OnPaidEvent | OnAdPaid |
| OnAdDidPresentFullScreenContent | OnAdFullScreenContentOpened |
| OnAdDidDismissFullScreenContent | OnAdFullScreenContentClosed |
| OnAdFailedToPresentFullScreenContent | OnAdFullScreenContentFailed |
| OnAdDidRecordImpression | OnAdImpressionRecorded |
AppOpenAd | |
| LoadAd() | AppOpenAd.Load() |
| OnPaidEvent | OnAdPaid |
| OnAdDidPresentFullScreenContent | OnAdFullScreenContentOpened |
| OnAdDidDismissFullScreenContent | OnAdFullScreenContentClosed |
| OnAdFailedToPresentFullScreenContent | OnAdFullScreenContentFailed |
| OnAdDidRecordImpression | OnAdImpressionRecorded |
AdErrorEventArgs | |
| AdErrorEventArgs.AdError | 請直接使用 AdError。 |
AdFailedToLoadEventArgs | |
| AdFailedToLoadEventArgs.LoadAdError | 直接使用 LoadAdError。 |
AdValueEventArgs | |
| AdValueEventArgs.AdValue | 直接使用 AdValue。 |