Admob causes IOS to lag after which crash


I’m fairly determined for steerage on this as I am new and can’t discover any posts like mine. My app works high quality on android however on iOS it stutters after displaying a number of adverts after which crashes. I do know this as a result of if I take away the adverts it really works nice. I am positive my implementation is improper however right here it’s. I’ve a number of totally different interstitials and rewarded adverts as a result of I wish to have totally different OnAdClosed habits relying on what button was clicked/transition spot the consumer is in once they see the advert. I think that is the issue however I am unsure code it in the suitable method. Or probably I’m not destroying the adverts within the appropriate place.

public class AdManager : MonoBehaviour
{
    public string levelMenu;
    personal InterstitialAd interstitial;
    personal InterstitialAd interstitialExit;
    personal RewardedAd rewardBasedVideoMain;
    personal RewardedAd rewardBasedVideoRush;
    personal RewardedAd rewardBasedVideoFind;
    public static AdManager occasion;
    public GameObject WinLoseObj;
    public GameObject PauseMenu;
    bool isRewarded = false;
    bool isRewardedRush = false;
    bool isRewardedFind = false;
    System.DateTime currentDate;
    personal void Awake(){
        if (occasion == null){
            occasion = this;
        } else {
            Destroy(gameObject);
            return;
        }
    }
    // Begin known as earlier than the primary body replace
    void Begin()
    {
        MobileAds.Initialize(InitializationStatus => { });
        this.RequestRewardBasedVideoMain();
        this.RequestRewardBasedVideoRush();
        this.RequestRewardBasedVideoFind();
        this.RequestInterstitial();
        this.RequestInterstitialExit();
    }
 
    personal AdRequest CreateAdRequest(){
        return new AdRequest.Builder().Construct();
    }
    public void RequestRewardBasedVideoMain() UNITY_STANDALONE_OSX
            adUnitId = "fake my advert unit id is right here";
        #endif
        this.rewardBasedVideoMain = new RewardedAd(adUnitId);
        this.rewardBasedVideoMain.OnUserEarnedReward += this.HandleRewardBasedVideoRewardedMain;
        this.rewardBasedVideoMain.OnAdClosed += this.HandleRewardBasedVideoClosedMain;
        this.rewardBasedVideoMain.LoadAd(this.CreateAdRequest());
    
    public void RequestRewardBasedVideoRush()
    public void RequestRewardBasedVideoFind() UNITY_STANDALONE_OSX
            adUnitId = "fake my advert unit id is right here";
        #endif
        this.rewardBasedVideoFind = new RewardedAd(adUnitId);
        this.rewardBasedVideoFind.OnUserEarnedReward += this.HandleRewardBasedVideoRewardedFind;
        this.rewardBasedVideoFind.OnAdClosed += this.HandleRewardBasedVideoClosedFind;
        this.rewardBasedVideoFind.LoadAd(this.CreateAdRequest());
    
    public void ShowRewardBasedVideoMain(){
        if (this.rewardBasedVideoMain.IsLoaded()){
            this.rewardBasedVideoMain.Present();
        } else {
            this.RequestRewardBasedVideoMain();
            string check = "Error, attempt once more later.";
            FindObjectOfType<mainBoosterManager>().mainBoostWatchFailed(check);
        }
    }
    public void ShowRewardBasedVideoRush(){
        if (this.rewardBasedVideoRush.IsLoaded()){
            this.rewardBasedVideoRush.Present();
        } else {
            this.RequestRewardBasedVideoRush();
            string check = "Error, attempt once more later.";
            FindObjectOfType<RushStart>().rushBoostWatchFailed(check);
        }
    }
    public void ShowRewardBasedVideoFind(){
        if (this.rewardBasedVideoFind.IsLoaded()){
            this.rewardBasedVideoFind.Present();
        } else {
            this.RequestRewardBasedVideoFind();
            string check = "Error, attempt once more later.";
            FindObjectOfType<FindStart>().findBoostWatchFailed(check);
        }
    }
 
    public void RequestInterstitial(){
        string adUnitId = "fake my advert unit id is right here";
        #if UNITY_IOS || UNITY_STANDALONE_OSX
            adUnitId = "fake my advert unit id is right here";
        #endif
 
        // Cleanup interstitial advert earlier than creating a brand new one
        if (this.interstitial != null){
            this.interstitial.Destroy();
        }
 
        // Create an interstitial
        this.interstitial = new InterstitialAd(adUnitId);
 
        // Load an interstitial advert
        this.interstitial.LoadAd(this.CreateAdRequest());
        this.interstitial.OnAdClosed += HandleAdClosed;
    }
    public void RequestInterstitialExit(){
        string adUnitId = "fake my advert unit id is right here";
        #if UNITY_IOS || UNITY_STANDALONE_OSX
            adUnitId = "fake my advert unit id is right here";
        #endif
 
        // Cleanup interstitial advert earlier than creating a brand new one
        if (this.interstitialExit != null){
            this.interstitialExit.Destroy();
        }
 
        // Create an interstitial
        this.interstitialExit = new InterstitialAd(adUnitId);
 
        // Load an interstitial advert
        this.interstitialExit.LoadAd(this.CreateAdRequest());
        this.interstitialExit.OnAdClosed += HandleAdClosedExit;
    }
    public void ShowInterstitial(){
        if (this.interstitial.IsLoaded()){
            interstitial.Present();
        } else {
            this.RequestInterstitial();
            Time.timeScale = 1f;
            if (WinLoseObj != null){
                WinLoseObj.SetActive(false);
            }
            PauseMenu.SetActive(false);
        }
    }
    public void ShowInterstitialExit(){
        if (this.interstitialExit.IsLoaded()){
            interstitialExit.Present();
        } else {
            this.RequestInterstitialExit();
            Time.timeScale = 1f;
            SceneManager.LoadScene(levelMenu);
        }
    }
    public void HandleAdClosed(object sender, EventArgs args)
    {
        Time.timeScale = 1f;
        if (WinLoseObj != null){
            WinLoseObj.SetActive(false);
        }
        PauseMenu.SetActive(false);
        RequestInterstitial();
    }
    public void HandleAdClosedExit(object sender, EventArgs args)
    {
        Time.timeScale = 1f;
        RequestInterstitialExit();
        SceneManager.LoadScene(levelMenu);
    }
    public void HandleRewardBasedVideoClosedMain(object sender, EventArgs args){
        this.RequestRewardBasedVideoMain();
    }
    public void HandleRewardBasedVideoRewardedMain(object sender, EventArgs args){
        isRewarded = true;
    }
    public void HandleRewardBasedVideoClosedRush(object sender, EventArgs args){
        this.RequestRewardBasedVideoRush();
    }
    public void HandleRewardBasedVideoRewardedRush(object sender, EventArgs args){
        isRewardedRush = true;
    }
    public void HandleRewardBasedVideoClosedFind(object sender, EventArgs args){
        this.RequestRewardBasedVideoFind();
    }
    public void HandleRewardBasedVideoRewardedFind(object sender, EventArgs args){
        isRewardedFind = true;
    }
    // Replace known as as soon as per body
    void Replace()
    {
        if (isRewarded){
            isRewarded = false;
            //improve boosts
            FindObjectOfType<mainBoosterManager>().mainBoostWatch();
        }
        if (isRewardedRush){
            isRewardedRush = false;
            //improve boosts
            FindObjectOfType<RushStart>().rushBoostWatch();
        }
        if (isRewardedFind){
            isRewardedFind = false;
            //improve boosts
            FindObjectOfType<FindStart>().findBoostWatch();
        }
    }
}

Similar Posts

Leave a Reply

Your email address will not be published.