上次更新時間: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);
}
廣告事件委派項現在會採用特定型別引數
在 API 第 7 版中,我們使用 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。 |