I have been trying for days to resolve a performance issue using IronSource.
If I load my ad manager and not play an ad, my game works without performance issues.
The moment an ad shows, the games performance is impacted.
Each ad after increasingly degrades performance.
using UnityEngine;
public class AdManager : MonoBehaviour
{
private const string _adId = "";
public static AdManager Instance;
public delegate void AdInterstitialCompletionHandler();
public event AdInterstitialCompletionHandler OnInterstitialAdCompleted;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
}
private void OnEnable()
{
IronSourceEvents.onInterstitialAdClosedEvent += InterstitialAdClosed;
}
private void OnDisable()
{
IronSourceEvents.onInterstitialAdClosedEvent -= InterstitialAdClosed;
}
void Start()
{
var purchasedAdRemoval = SettingsManager.PurchasedRemoveAds();
if (!purchasedAdRemoval)
{
IronSource.Agent.init(_adId);
LoadInterstitial();
}
}
private void LoadInterstitial()
{
Debug.Log("Loading interstitial...");
IronSource.Agent.loadInterstitial();
}
public void TryShowInterstitialAd()
{
if (SettingsManager.PurchasedRemoveAds())
{
Debug.Log("Ads removed by user purchase.");
OnInterstitialAdComplete();
}
else
{
SettingsManager.AddGamePlayedSinceLastAd();
int gamesPlayed = SettingsManager.GetGamesPlayedSinceLastAd();
Debug.Log($"Games played since last ad: {gamesPlayed}");
if (gamesPlayed >= 3 && IronSource.Agent.isInterstitialReady())
{
Debug.Log($"Showing interstitial ad");
IronSource.Agent.showInterstitial();
}
else
{
Debug.Log("Not enough games played since last ad to show interstitial.");
OnInterstitialAdComplete();
}
}
}
private void InterstitialAdClosed()
{
SettingsManager.ResetGamesPlayedSinceLastAd();
LoadInterstitial();
OnInterstitialAdComplete();
}
void OnApplicationPause(bool isPaused)
{
IronSource.Agent.onApplicationPause(isPaused);
}
private void OnInterstitialAdComplete()
{
OnInterstitialAdCompleted?.Invoke();
}
}
Newer version did not work. Performance still takes a hit after an ad has loaded.
using UnityEngine;
public class AdManager : MonoBehaviour
{
private const string _adId = "";
public static AdManager Instance;
public delegate void AdInterstitialCompletionHandler();
public event AdInterstitialCompletionHandler OnInterstitialAdCompleted;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
}
private void OnEnable()
{
IronSourceInterstitialEvents.onAdReadyEvent += InterstitialOnAdReadyEvent;
IronSourceInterstitialEvents.onAdLoadFailedEvent += InterstitialOnAdLoadFailed;
IronSourceInterstitialEvents.onAdOpenedEvent += InterstitialOnAdOpenedEvent;
IronSourceInterstitialEvents.onAdClickedEvent += InterstitialOnAdClickedEvent;
IronSourceInterstitialEvents.onAdShowSucceededEvent += InterstitialOnAdShowSucceededEvent;
IronSourceInterstitialEvents.onAdShowFailedEvent += InterstitialOnAdShowFailedEvent;
IronSourceInterstitialEvents.onAdClosedEvent += InterstitialOnAdClosedEvent;
}
private void OnDisable()
{
IronSourceInterstitialEvents.onAdReadyEvent -= InterstitialOnAdReadyEvent;
IronSourceInterstitialEvents.onAdLoadFailedEvent -= InterstitialOnAdLoadFailed;
IronSourceInterstitialEvents.onAdOpenedEvent -= InterstitialOnAdOpenedEvent;
IronSourceInterstitialEvents.onAdClickedEvent -= InterstitialOnAdClickedEvent;
IronSourceInterstitialEvents.onAdShowSucceededEvent -= InterstitialOnAdShowSucceededEvent;
IronSourceInterstitialEvents.onAdShowFailedEvent -= InterstitialOnAdShowFailedEvent;
IronSourceInterstitialEvents.onAdClosedEvent -= InterstitialOnAdClosedEvent;
}
void Start()
{
IronSourceEvents.onSdkInitializationCompletedEvent += SdkInitializationCompletedEvent;
IronSource.Agent.validateIntegration();
var purchasedAdRemoval = SettingsManager.PurchasedRemoveAds();
if (!purchasedAdRemoval)
{
IronSource.Agent.init(_adId);
LoadInterstitial();
}
}
private void SdkInitializationCompletedEvent()
{
Debug.Log("IronSource Sdk Initialization Completed");
}
private void LoadInterstitial()
{
Debug.Log("Loading interstitial...");
IronSource.Agent.loadInterstitial();
}
public void TryShowInterstitialAd()
{
if (SettingsManager.PurchasedRemoveAds())
{
Debug.Log("Ads removed by user purchase.");
OnInterstitialAdCompleted?.Invoke();
}
else
{
SettingsManager.AddGamePlayedSinceLastAd();
int gamesPlayed = SettingsManager.GetGamesPlayedSinceLastAd();
Debug.Log($"Games played since last ad: {gamesPlayed}");
if (gamesPlayed >= 3 && IronSource.Agent.isInterstitialReady())
{
Debug.Log($"Showing interstitial ad");
IronSource.Agent.showInterstitial();
}
else
{
Debug.Log("Not enough games played since last ad to show interstitial.");
OnInterstitialAdCompleted?.Invoke();
}
}
}
void OnApplicationPause(bool isPaused)
{
IronSource.Agent.onApplicationPause(isPaused);
}
/************* Interstitial AdInfo Delegates *************/
// Invoked when the interstitial ad was loaded succesfully.
void InterstitialOnAdReadyEvent(IronSourceAdInfo adInfo)
{
}
// Invoked when the initialization process has failed.
void InterstitialOnAdLoadFailed(IronSourceError ironSourceError)
{
}
// Invoked when the Interstitial Ad Unit has opened. This is the impression indication.
void InterstitialOnAdOpenedEvent(IronSourceAdInfo adInfo)
{
}
// Invoked when end user clicked on the interstitial ad
void InterstitialOnAdClickedEvent(IronSourceAdInfo adInfo)
{
}
// Invoked when the ad failed to show.
void InterstitialOnAdShowFailedEvent(IronSourceError ironSourceError, IronSourceAdInfo adInfo)
{
OnInterstitialAdCompleted?.Invoke();
}
// Invoked when the interstitial ad closed and the user went back to the application screen.
void InterstitialOnAdClosedEvent(IronSourceAdInfo adInfo)
{
SettingsManager.ResetGamesPlayedSinceLastAd();
LoadInterstitial();
OnInterstitialAdCompleted?.Invoke();
}
// Invoked before the interstitial ad was opened, and before the InterstitialOnAdOpenedEvent is reported.
// This callback is not supported by all networks, and we recommend using it only if
// it's supported by all networks you included in your build.
void InterstitialOnAdShowSucceededEvent(IronSourceAdInfo adInfo)
{
}
}
There’s a lot going on in the script you showed, and the problem could lay anywhere. You need to use the Profiler and see what is taking so much time, isolate it, then try to fix it/find a workaround.
Finally figured out the issue, sorta. I have a fix but I don’t understand what is happening.
I added an FPS counter and saw the game would always run at 30 fps.
When I play an ad, it drops to 15 fps.
Added Application.targetFrameRate = 60, and now it works great.