0

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);
        }


    }
Programmer
  • 121,791
  • 22
  • 236
  • 328

2 Answers2

0

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Must call interstitial.destroy ();, if I use advertising at every third level? –  Aug 11 '16 at 19:44
  • You sound like you don't understand my answer. You can ask question if any part of it is confusing. If you do `interstitial = new InterstitialAd(adUnitId);`, you have to do `interstitial.destroy();` too in the `OnDestroy` or `OnDisable` function. I showed you a way to to have only one `GoogleAdsScript` in your scene that contains everything. You won't have to destroy it if you follow that direction in my answer – Programmer Aug 11 '16 at 19:55
  • I change script: `void Update() { if (interstitial.IsLoaded() && isLoaded == false) { interstitial.Show(); isLoaded = true; } } void OnDestroy() { if (interstitial.IsLoaded()) interstitial.Destroy(); }` Is it the clean way? –  Aug 11 '16 at 20:21
  • That's what I meant in the first part of my answer but I suggest you do the second part instead because what you are doing is different. Since you want to show ad when closing and new level is starting,it won't be a good idea to call `interstitial.Destroy()` when level is Destoying. You should only do this if there is a button that the user clicks that will load the next level. If the level loads automatically, the user may not have the chance to see the ad. Go with the second solution – Programmer Aug 11 '16 at 20:38
  • There is a button for next level, not loads automatically –  Aug 11 '16 at 20:40
  • Is the second method better for operation memory? –  Aug 11 '16 at 20:52
  • **"There is a button for next level, not loads automatically"** That's fine then. **"Is the second method better for operation memory?**" Yes. I was going to say that next. That's because you are not creating and destroying objects. You can go with which ever one you want but the second one is better in terms of memory management. – Programmer Aug 11 '16 at 20:59
  • so, when i use second way, for hiding ads I hiding AdsObj (`AdsObj.setActive(true)`) or which way to active or hiding this ad? –  Aug 11 '16 at 21:03
  • Add a new `public` function that hides ad in your `GoogleAdsScript` class. Simply put `bannerView.Hide()` in it. That's it. – Programmer Aug 11 '16 at 21:08
  • sorry, i am stupid, I actually don't need interstitial.Hide() :D, because it is cancel by user :D –  Aug 11 '16 at 21:59
  • Lol I thought about that too. Your problem should be solved by now – Programmer Aug 11 '16 at 22:17
  • One last thing: How many times should call the function RequestInterstitial? Before every interstitial.Show() or only on the start of game? Thanks :) –  Aug 11 '16 at 22:23
  • You need to separate your the code in `RequestInterstitial()` into 2. The part to put into another function is `// Initialize an InterstitialAd. interstitial = new InterstitialAd(adUnitId);`. Put it in a function called `init()` You can call this function just once. Also put `interstitial.Show();` in another function called `show()`. To show ad, call `RequestInterstitial()` then `show()` funct. Do the-same thing for your `RequestBanner()` function. Put `interstitial = new InterstitialAd(adUnitId);` in the init function and write a custom `show()` function for it. Happy coding! – Programmer Aug 11 '16 at 22:53
  • @Adam You can accept this answer if your problem is solved. – Programmer Aug 12 '16 at 11:27
0

This worked for me...

Pseudo:

  1. Create a static ad manager script (see example below)
  2. Request the interstitial ad at the beginning of the level.
  3. When the level is completed, check if the ad is loaded then show it.
  4. Just after that, load your next level or menu scene (DO NOT DESTROY THE INTERSTITIAL AD!)

Example of static AdManagerScript:

using GoogleMobileAds.Api;

public static class AdManagerScript
{
    private static InterstitialAd interstitial;

    // Call this method only once when your app starts
    public static void StartMobileAdsSDK()
    {
        #if UNITY_ANDROID
            string appId = "...your admob appid here...";
            MobileAds.Initialize(appId);
        #endif
    }

    public static void RequestInterstitial()
    {
        #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/1033173712";
            interstitial = new InterstitialAd(adUnitId);
            AdRequest request = new AdRequest.Builder().Build();
            interstitial.LoadAd(request);
        #endif
    }

    public static void ShowInterstitital()
    {
        #if UNITY_ANDROID
            if (interstitial.IsLoaded())
            {
                interstitial.Show();
            }
        #endif
    }

Next, in your level script:

...

void Start() {
    AdManager.RequestInterstitial();
}

...

void GameLevelFinished() {
    AdManager.ShowInterstitial();
    SceneManager.LoadScene("NextLevelName");
}

Also, remember to call the StartMobileAdsSDK once in the beginning when your app starts.

Admanager.StartMobileAdsSDK();

Use the precompiler directives (#if #endif) to avoid trying to load ads in the Unity editor. There will be some messages in the console, but that's normal.

James
  • 31
  • 4