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.");
    }
}

使用 Show(Action) 展示激励广告

在插件版本 7 中,激励广告使用带单独 OnUserEarnedReward 事件的 Show() 方法处理用户奖励信号,插页式激励广告则使用带回调的 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 方法,以便在所有广告格式之间保持一致。

下表列出了 v8 中引入的类变更。

BannerView
v7v8
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