Ultimo aggiornamento: febbraio 2023
Questa pagina riguarda le migrazioni delle versioni attuali e precedenti.
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 caricare gli 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 (precedente)
#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 (precedente)
// 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());
}
}
Utilizzare CanShowAd() per verificare se è possibile mostrare annunci a schermo intero
Nella versione 7, gli annunci a schermo intero (annunci interstitial, con premio, interstitial con premio e apertura app) avevano il metodo IsLoaded()
che restituisce true
se l'annuncio era stato caricato. A causa della modifica nella modalità di caricamento degli annunci, nella versione 8 non hai accesso all'oggetto dell'annuncio a schermo intero finché l'annuncio non viene caricato. Di conseguenza, il metodo IsLoaded()
è obsoleto.
La versione 8 ha un nuovo metodo denominato CanShowAd()
che restituisce true
se l'annuncio può ancora essere mostrato. Ecco un esempio di come utilizzare CanShowAd()
su 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 (precedente)
private InterstitialAd _interstitalAd;
public void ShowInterstitialAd()
{
if (_interstitalAd != null && _interstitalAd.IsLoaded())
{
_interstitalAd.Show();
}
else
{
Debug.Log("Interstitial ad is not ready yet.");
}
}
Utilizza Show(Action) 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 del premio per l'utente, mentre l'annuncio interstitial con premio aveva un metodo Show(Action<Reward>)
con un callback per gestire l'indicatore del premio per l'utente. 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 relativa al 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 (precedente)
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 per gli eventi annuncio ora adottano argomenti di tipo specifici
Nella versione 7 dell'API, abbiamo utilizzato EventHandlers
per la definizione dei delegati per gli eventi. Nella versione 8, abbiamo adottato i delegati generici per gli eventi annuncio. Di conseguenza, gli eventi ora emettono direttamente i valori degli eventi 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 (precedente)
private BannerView _bannerView;
public void ConfigureBanner()
{
_bannerView.OnPaidEvent += (object sender, AdValueEventArg arg) =>
{
AdValue value = arg.Value;
};
}
Ora i formati degli annunci sono 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 dei nostri metodi API per essere coerenti nei formati degli annunci.
Nella tabella seguente sono elencate le modifiche alla classe introdotte nella versione 8.
BannerView | |
---|---|
v7 | 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() |
Annuncio con premio() | RewardedAd.Load() |
OnAdLoaded | RewardedAd.Load() |
OnAdFailedToLoad | RewardedAd.Load() |
OnAdOpening | OnAdFullScreenContentOpened |
OnAdClosed | OnAdFullScreenContentClosed |
OnAdFailedToShow | OnAdFullScreenContentFailed |
OnAdDidRecordImpression | OnAdImpressionRecorded |
OnPaidEvent | OnAdPaid |
Mostra() | 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. |