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 では、広告が読み込まれると true を返す IsLoaded() メソッドがフルスクリーン広告(インタースティシャル広告、リワード広告、リワード インタースティシャル広告、アプリ起動時広告)で使用されていました。バージョン 8 では、広告の読み込み方法が変更されたため、広告が読み込まれるまでフルスクリーン広告オブジェクトにアクセスできないため、IsLoaded() メソッドは廃止されました。

バージョン 8 には、広告を表示できる場合に true を返す CanShowAd() という新しいメソッドが追加されています。インタースティシャル広告で 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);
}

広告イベントのデリゲートで特定のタイプの引数が採用されるようになりました

バージョン 7 の API では、イベント デリゲートを定義する際に EventHandlers を使用していました。バージョン 8 では、広告イベントに汎用デリゲートが採用されました。その結果、イベントは EventArg クラスにラップされることなく、イベント値を直接出力するようになります。

次に、(OnPaidEvent の代わりに)OnAdPaid を使用する例を示します。

バージョン 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 を直接使用します。