SDK 遷移

上次更新時間: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.");
    }
}

使用節目(動作)顯示獎勵廣告

在第 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
v7v8
OnAdLoaded OnBannerAdLoaded
OnAdFailedToLoad OnBannerAdLoadFailed
OnAdOpening OnAdFullScreenContentOpened
OnAdClosed OnAdFullScreenContentClosed
OnPaidEvent OnAdPaid
InterstitialAd
LoadAd() InterstitialAd.Load()
插頁式廣告() InterstitialAd.Load()
OnAdLoaded InterstitialAd.Load()
OnAdFailedToLoad InterstitialAd.Load()
OnAdOpening OnAdFullScreenContentOpened
OnAdClosed OnAdFullScreenContentClosed
OnAdFailedToShow OnAdFullScreenContentFailed
OnAdDidRecordImpression OnAdImpressionRecorded
OnPaidEvent OnAdPaid
RewardedAd
LoadAd() RewardedAd.Load()
RewardsAd() 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