I updated the unity ads package to 4.0 and they got rid of some functions and now I cant get the ads to work. I’ve tried a few different ways but nothing has worked and I couldn’t find anything helpful online here’s what I have right now.
Please let me know if there’s something I’m missing, they dont have the AddListener() function anymore and I dont know if theres something else I have too do?
Hi, for now, we need to use new interfaces for unity ads, that implement all needed features!
Here is my code example that works!
In your case, you are missing the third param of Advertisement.Initialize(_gameId, _testMode,this); where “this” is the class which uses IUnityAdsInitializationListener interface.
public class MonetizationService : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{
private Action OnAdFinished;
private Action OnAdSkipped;
private Action OnAdError;
public static MonetizationService Instance;
[SerializeField] string _androidGameId = "YOUR_ANDROID_GAME_ID";
[SerializeField] string _iOSGameId = "YOUR_IOS_GAME_ID";
[SerializeField] bool _testMode = true;
private string _gameId;
[SerializeField] string _androidAdUnitId = "Interstitial_Android";
[SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
private string _adUnitId;
private bool isInitialized = false;
private void Awake()
{
if (Instance == null)
{
Instance = this;
_gameId = (Application.platform == RuntimePlatform.IPhonePlayer) ? _iOSGameId : _androidGameId;
Advertisement.Initialize(_gameId, _testMode,this);
}
else
{
Destroy(this.gameObject);
}
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
isInitialized = false;
}
public void OnInitializationComplete()
{
isInitialized = true;
_adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer) ? _iOsAdUnitId : _androidAdUnitId;
Advertisement.Load(_adUnitId, this);
Debug.Log("[MonetizationService] Loading Ad: " + _adUnitId);
}
// Implement Load Listener and Show Listener interface methods:
public void OnUnityAdsAdLoaded(string adUnitId)
{
Debug.Log("[MonetizationService] Advertisement was successfully loaded!id = " + _adUnitId);
// Optionally execute code if the Ad Unit successfully loads content.
}
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
{
Debug.LogError($"[MonetizationService] Error loading Ad Unit: {adUnitId} - {error.ToString()} - {message}");
// Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
}
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
{
Debug.LogError($"[MonetizationService] Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
// Optionally execute code if the Ad Unit fails to show, such as loading another ad.
OnAdError?.Invoke();
}
public void OnUnityAdsShowStart(string adUnitId)
{
Debug.Log("[MonetizationService] Advertisement show started! id = " + _adUnitId);
}
public void OnUnityAdsShowClick(string adUnitId)
{
Debug.Log("[MonetizationService] Advertisement clicked! id = " + _adUnitId);
}
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
Debug.Log("[MonetizationService] Advertisement completed with result! result = " + showCompletionState);
if (showCompletionState == UnityAdsShowCompletionState.COMPLETED)
{
OnAdFinished?.Invoke();
}
else if (showCompletionState == UnityAdsShowCompletionState.SKIPPED)
{
OnAdSkipped?.Invoke();
}
else if(showCompletionState == UnityAdsShowCompletionState.UNKNOWN)
{
OnAdError?.Invoke();
}
}
public void ShowAd(Action onAdFinished = null, Action onAdSkipped = null, Action onAdError = null)
{
Debug.Log($"[MonetizationService] ShowAd");
OnAdFinished = onAdFinished;
OnAdSkipped = onAdSkipped;
OnAdError = onAdError;
if (!isInitialized)
{
Debug.LogError($"[MonetizationService] Error showing Ad Unit - monetization is not initialized!");
onAdError?.Invoke();
return;
}
Advertisement.Show(_adUnitId, this);
}
}