Search code examples
c#unity-game-engineadsinterstitialunityads

InterstitialAds in Unity


I have this code for InterstitialAds in Unity and I want to start this fullscreen ads everytime, when level is closing and new level is starting, so I use OnDestroy function, but when I must call interstitial.destroy(); ? Between: Is the code right for the smooth running of the game?? Thanks for all answer and sorry for my english :)

    public class GoogleAdsScript : MonoBehaviour
    {
        bool isLoaded = false;
        private InterstitialAd interstitial;
        private BannerView bannerView;

        void Start()
        {
            RequestInterstitial();

            //RequestBanner();
        }

        void OnDestroy()
        {
            if (interstitial.IsLoaded() && isLoaded == false)
            {
                interstitial.Show();
                isLoaded = true;
            }
        }

        private void RequestInterstitial()
        {
    #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/1033173712";
    #elif UNITY_IPHONE
            string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
    #else
            string adUnitId = "unexpected_platform";
    #endif

            // Initialize an InterstitialAd.
             interstitial = new InterstitialAd(adUnitId);
            // Create an empty ad request.
            AdRequest request = new AdRequest.Builder().Build();
            // Load the interstitial with the request.
            interstitial.LoadAd(request);
        }

        private void RequestBanner()
        {
    #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/6300978111";
    #elif UNITY_IPHONE
            string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
    #else
            string adUnitId = "unexpected_platform";
    #endif

            // Create a 320x50 banner at the top of the screen.
            bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
            // Create an empty ad request.
            AdRequest request = new AdRequest.Builder().Build();
            // Load the banner with the request.
            bannerView.LoadAd(request);
        }


    }

Solution

  • If the script(GoogleAdsScript) that is holding the InterstitialAd instance reference(interstitial) is about to Destroy, you should call interstitial.destroy();. You do that so that you won't lose the reference.

    My suggestion is to make important functions in the GoogleAdsScript script to be public. Attach GoogleAdsScript to GameObject called AdsObj. Put DontDestroyOnLoad(transform.gameObject); in the Awake() function of GoogleAdsScript script so that it won't destroy when loading new scene. You can now access GoogleAdsScript from other scripts to show or hide ads.

    public class OtherScript : MonoBehaviour
    {
        public GoogleAdsScript googleAds;
    
        void Start()
        {
            googleAds = GameObject.Find("AdsObj").GetComponent<GoogleAdsScript>();
            googleAds.RequestInterstitial();//Assumes that RequestInterstitial is now public
        }
    }
    

    There is no reason to destroy the GoogleAdsScript script anymore.