how to make unityad close game on "close"

so this code would normally exit the game, but ive added so that on exit it shows ad first. i want it to close the game once ad is finished but i cant figure out how to do this. help please #learner

using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;


public class QuitApplication : AdManager {


    public void Quit()
    {
        Advertisement.Initialize ("64010", true);
     
        StartCoroutine (ShowAdWhenReady());
        //If we are running in a standalone build of the game
    #if UNITY_STANDALONE
        //Quit the application
        Application.Quit();
    #endif

        //If we are running in the editor
    #if UNITY_EDITOR
        //Stop playing the scene
        //UnityEditor.EditorApplication.isPlaying = false;

    #endif
    }

    IEnumerator ShowAdWhenReady()
    {
        while (!Advertisement.isReady())
            yield return null;
     
        Advertisement.Show ();

 
        //UnityEditor.EditorApplication.isPlaying = false;
    }


}

check out the WaitForAd() function in the AdManager script here:

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/integrating-ads

how would i write this so that thisUnityEditor.EditorApplication.isPlaying = false; happens if ad is not showing while (Advertisement.isShowing

WaitForAd is a coroutine, if you want something to happen after the ad has finished I think you just need

StartCoroutine(WaitForAd ());
// your thing to happen after the ad has finished here

thanks for your help much appreciated

using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;


public class QuitApplication : AdManager {


    public void Quit()
    {
        Advertisement.Initialize ("64010", true);
       
        StartCoroutine (ShowAdWhenReady());
        StartCoroutine (WaitForAd ());
    IEnumerator WaitForAd()
    {
        float currentTimeScale = Time.timeScale;
        Time.timeScale = 0f;
        yield return null;
       
        while (Advertisement.isShowing)
            yield return null;
        //this got it working
        UnityEditor.EditorApplication.isPlaying = false;

        Time.timeScale = currentTimeScale;
    }

Just to be a little pedantic; but putting that line inside the coroutine means the coroutine cannot be used anywhere else, it’s also no longer just “wait for ad” but “close on ad finish”. It will work, but it’s not as clear should you want to extend the project later or someone else looks at it, or you copy the code out into another project without a thorough review etc.

Personally I’d put what you’ve got on line 10 in the second code chunk on line 15 of the first chunk.

Quit() would then read:
initialise ad,
show ad when ready,
wait for ad,
quit

and WaitForAd() can be used anywhere else you might want it to.

yeah thanks ill do that

File those under" ways to piss off players one last time when they try to close my game"

2 Likes

Yup. You generally want to make quitting as painless as possible. Once the player has hit quit they are trying to move onto their life stuff.

It’s really a moot point anyway, UnityAds are only for mobile, and you don’t explicitly quit mobile aps anyway. The user can take control and boot your game whenever they feel like it. And chances are they won’t use the quit button.

I prefer putting an ad onto loading screen, this prefer only piss up the player, watching an ad to get free stuff is ok, but to quit… Most of the players would only quit with home button.

I’m not fussed it shows ad using back button or on exit before it closes app. I don’t have any ads at start or during game so no interruption in game play. This is my very first game ever so I just have to see how things go haha