Migrazione degli SDK

Ultimo aggiornamento: febbraio 2023

Questa pagina illustra le migrazioni per la versione attuale e quella precedente.

Migrazione dalla versione 7 alla versione 8

I formati a schermo intero ora utilizzano un metodo di caricamento statico

Nella versione 7 del plug-in, gli annunci interstitial e con premio avevano un metodo LoadAd() a livello di istanza per caricare un annuncio, mentre gli annunci interstitial con premio e gli annunci apertura app avevano un metodo Load() statico per il caricamento degli annunci. Nella versione 8, tutti i formati degli annunci a schermo intero (interstitial, con premio, interstitial con premio e apertura app) avranno un metodo Load() statico per caricare gli annunci. Ecco un esempio di come caricare un annuncio interstitial:

Versione 8 (attuale)

#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;
        });
}

Versione 7 (legacy)

#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());
    }
}

Ecco un esempio di come caricare un annuncio con premio:

Versione 8 (attuale)

// 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;
        });
}

Versione 7 (legacy)

// 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());
    }
}

Utilizza CanShowAd() per verificare se è pronto a mostrare annunci a schermo intero

Nella versione 7, gli annunci a schermo intero (interstitial, con premio, interstitial con premio e annunci apertura app) avevano il metodo IsLoaded() che restituisce true se l'annuncio è stato caricato. A causa del cambiamento nella modalità di caricamento degli annunci, nella versione 8 non hai accesso all'oggetto annuncio a schermo intero fino al caricamento dell'annuncio, rendendo obsoleto il metodo IsLoaded().

La versione 8 presenta un nuovo metodo, denominato CanShowAd(), che restituisce true se l'annuncio può essere ancora visualizzato. Ecco un esempio di come utilizzare CanShowAd() in un annuncio interstitial:

Versione 8 (attuale)

private InterstitialAd _interstitalAd;

public void ShowInterstitialAd()
{
    if (_interstitalAd != null && _interstitalAd.CanShowAd())
    {
        _interstitalAd.Show();
    }
    else
    {
        Debug.Log("Interstitial ad cannot be shown.");
    }
}

Versione 7 (legacy)

private InterstitialAd _interstitalAd;

public void ShowInterstitialAd()
{
    if (_interstitalAd != null && _interstitalAd.IsLoaded())
    {
        _interstitalAd.Show();
    }
    else
    {
        Debug.Log("Interstitial ad is not ready yet.");
    }
}

Utilizza Mostra(Azione) per mostrare gli annunci con premio

Nella versione 7 del plug-in, gli annunci con premio avevano il metodo Show() con un evento OnUserEarnedReward separato per la gestione degli indicatori premio per gli utenti, mentre l'annuncio interstitial con premio aveva un metodo Show(Action<Reward>) con un callback per gestire l'indicatore premio per gli utenti. Nella versione 8, i formati degli annunci interstitial con premio e con premio avranno un metodo Show(Action<Reward>) con un callback per gestire la notifica del premio per l'utente.

Ecco un esempio di come mostrare un annuncio con premio:

Versione 8 (attuale)

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

Versione 7 (legacy)

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

I delegati agli eventi annuncio ora adottano argomenti di tipo specifici

Nella versione 7 dell'API, abbiamo utilizzato EventHandlers per definire i delegati dell'evento. Nella versione 8, abbiamo adottato i delegati generici per gli eventi relativi agli annunci. Di conseguenza, gli eventi ora emettono valori evento direttamente senza essere aggregati nella classe EventArg.

Ecco un esempio di utilizzo di OnAdPaid (al posto di OnPaidEvent):

Versione 8 (attuale)

private BannerView _bannerView;

public void ConfigureBanner()
{
    _bannerView.OnAdPaid += (AdValue value) =>
    {
        AdValue value = value;
    };
}

Versione 7 (legacy)

private BannerView _bannerView;

public void ConfigureBanner()
{
    _bannerView.OnPaidEvent += (object sender, AdValueEventArg arg) =>
    {
        AdValue value = arg.Value;
    };
}

I formati degli annunci sono ora conformi a un'interfaccia uniforme

Nella versione 7 del plug-in erano presenti discrepanze nei nomi degli eventi tra i formati degli annunci a schermo intero. Nella versione 8, abbiamo rinominato molti metodi dell'API per essere coerenti nei formati degli annunci.

La tabella seguente elenca le modifiche alle classi introdotte nella versione 8.

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()
RewardAd() RewardedAd.Load()
OnAdLoaded RewardedAd.Load()
OnAdFailedToLoad RewardedAd.Load()
OnAdOpening OnAdFullScreenContentOpened
OnAdClosed OnAdFullScreenContentClosed
OnAdFailedToShow OnAdFullScreenContentFailed
OnAdDidRecordImpression OnAdImpressionRecorded
OnPaidEvent OnAdPaid
Funzione Show() Mostra()
OnUserEarnedReward Mostra()
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 Utilizza direttamente AdError.
AdFailedToLoadEventArgs
AdFailedToLoadEventArgs.LoadAdError Utilizza direttamente LoadAdError.
AdValueEventArgs
AdValueEventArgs.AdValue Utilizza direttamente AdValue.