Unity LevelPlay è diventato da poco il sistema principale per la gestione degli annunci di Unity (dopo l’acquisizione avvenuta nel 2023 di ironSource).
Per iniziare a sviluppare la soluzione integrata di gestione degli annunci di Unity seguire la seguente nostra guida;
può essere utile anche seguire questa guida ufficiale di integrazione.
Di seguito invece un codice di partenza per l’implementazione di un Interstitial ADS di Unity Level Play Ads Mediation.
using UnityEngine;
using Unity.Services.LevelPlay;
public class AdsMediationManager : MonoBehaviour
{
private LevelPlayInterstitialAd interstitialAd;
[Header("LevelPlay Settings")]
public string AndroidAppKey = "23b121905";
public string IOSAppKey = "23b137235";
public bool IsTest = true;
public bool Logs = true;
[Header("Interstitial Settings")]
public string AndroidInterstitialAdUnitId = "led3r7a68mwqvy35";
public string IOSInterstitialAdUnitId = "l79yflyre1e1akb5";
void Start()
{
LevelPlay.OnInitSuccess += LevelPlay_OnInitSuccess; ;
LevelPlay.OnInitFailed += LevelPlay_OnInitFailed;
#if UNITY_ANDROID
LevelPlay.Init(this.AndroidAppKey);
#elif UNITY_IOS
LevelPlay.Init(this.AppKeyIOS);
#endif
}
private void LevelPlay_OnInitFailed(LevelPlayInitError obj)
{
if (this.Logs) Debug.LogWarning("LevelPlay Init Failed: " + obj.ErrorMessage.ToString());
}
private void LevelPlay_OnInitSuccess(LevelPlayConfiguration obj)
{
if (this.Logs) Debug.Log("LevelPlay Init Success - IsAdQualityEnabled: " + obj.IsAdQualityEnabled);
if (this.IsTest)
{
LevelPlay.SetMetaData("is_test_suite", "enable");
LevelPlay.LaunchTestSuite();
if (this.Logs) Debug.LogWarning("LevelPlay Test Suite: Enabled");
}
this.CreateInterstitialAd();
}
void CreateInterstitialAd()
{
//Create InterstitialAd instance
#if UNITY_ANDROID
interstitialAd = new LevelPlayInterstitialAd(this.AndroidInterstitialAdUnitId);
#elif UNITY_IOS
interstitialAd = new LevelPlayInterstitialAd(this.IOSInterstitialAdUnitId);
#endif
//Subscribe InterstitialAd events
interstitialAd.OnAdLoaded += InterstitialOnAdLoadedEvent;
interstitialAd.OnAdLoadFailed += InterstitialOnAdLoadFailedEvent;
interstitialAd.OnAdDisplayed += InterstitialOnAdDisplayedEvent;
interstitialAd.OnAdDisplayFailed += InterstitialOnAdDisplayFailedEvent;
interstitialAd.OnAdClicked += InterstitialOnAdClickedEvent;
interstitialAd.OnAdClosed += InterstitialOnAdClosedEvent;
interstitialAd.OnAdInfoChanged += InterstitialOnAdInfoChangedEvent;
}
public void LoadInterstitialAd()
{
//Load or reload InterstitialAd
interstitialAd.LoadAd();
}
void InterstitialOnAdLoadedEvent(LevelPlayAdInfo adInfo)
{
if (interstitialAd.IsAdReady())
{
interstitialAd.ShowAd();
if (this.Logs) Debug.Log("Interstitial ShowAd: " + adInfo.AdUnitId.ToString());
}
else
{
if (this.Logs) Debug.LogWarning("Interstitial Not Ready: " + adInfo.AdUnitId.ToString());
}
}
void InterstitialOnAdLoadFailedEvent(LevelPlayAdError ironSourceError){
if (this.Logs) Debug.LogWarning("Interstitial Load Failed: " + ironSourceError.ErrorMessage.ToString());
}
void InterstitialOnAdClickedEvent(LevelPlayAdInfo adInfo) {
if (this.Logs) Debug.Log("Interstitial Clicked: " + adInfo.AdUnitId.ToString());
}
void InterstitialOnAdDisplayedEvent(LevelPlayAdInfo adInfo) {
if (this.Logs) Debug.Log("Interstitial Displayed Successful: " + adInfo.AdUnitId.ToString());
}
void InterstitialOnAdDisplayFailedEvent(LevelPlayAdInfo adInfo, LevelPlayAdError error) {
if (this.Logs) Debug.LogWarning("Interstitial Displayed Failed: " + adInfo.AdUnitId.ToString());
}
void InterstitialOnAdClosedEvent(LevelPlayAdInfo adInfo) {
if (this.Logs) Debug.Log("Interstitial Closed: " + adInfo.AdUnitId.ToString());
}
void InterstitialOnAdInfoChangedEvent(LevelPlayAdInfo adInfo) {
if (this.Logs) Debug.LogWarning("Interstitial Info Changed: " + adInfo.AdUnitId.ToString());
}
}

