Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how to make unityad close game on "close"

Discussion in 'Scripting' started by gilmour, Aug 21, 2015.

  1. gilmour

    gilmour

    Joined:
    Aug 16, 2015
    Posts:
    6
    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


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Advertisements;
    4.  
    5.  
    6. public class QuitApplication : AdManager {
    7.  
    8.  
    9.     public void Quit()
    10.     {
    11.         Advertisement.Initialize ("64010", true);
    12.      
    13.         StartCoroutine (ShowAdWhenReady());
    14.         //If we are running in a standalone build of the game
    15.     #if UNITY_STANDALONE
    16.         //Quit the application
    17.         Application.Quit();
    18.     #endif
    19.  
    20.         //If we are running in the editor
    21.     #if UNITY_EDITOR
    22.         //Stop playing the scene
    23.         //UnityEditor.EditorApplication.isPlaying = false;
    24.  
    25.     #endif
    26.     }
    27.  
    28.     IEnumerator ShowAdWhenReady()
    29.     {
    30.         while (!Advertisement.isReady())
    31.             yield return null;
    32.      
    33.         Advertisement.Show ();
    34.  
    35.  
    36.         //UnityEditor.EditorApplication.isPlaying = false;
    37.     }
    38.  
    39.  
    40. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. gilmour

    gilmour

    Joined:
    Aug 16, 2015
    Posts:
    6
    how would i write this so that this
    Code (CSharp):
    1. UnityEditor.EditorApplication.isPlaying = false;
    happens if ad is not showing
    Code (CSharp):
    1. while (Advertisement.isShowing
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    WaitForAd is a coroutine, if you want something to happen after the ad has finished I think you just need

    Code (csharp):
    1.  
    2. StartCoroutine(WaitForAd ());
    3. // your thing to happen after the ad has finished here
    4.  
     
  5. gilmour

    gilmour

    Joined:
    Aug 16, 2015
    Posts:
    6
    thanks for your help much appreciated


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Advertisements;
    4.  
    5.  
    6. public class QuitApplication : AdManager {
    7.  
    8.  
    9.     public void Quit()
    10.     {
    11.         Advertisement.Initialize ("64010", true);
    12.        
    13.         StartCoroutine (ShowAdWhenReady());
    14.         StartCoroutine (WaitForAd ());
    Code (CSharp):
    1.     IEnumerator WaitForAd()
    2.     {
    3.         float currentTimeScale = Time.timeScale;
    4.         Time.timeScale = 0f;
    5.         yield return null;
    6.        
    7.         while (Advertisement.isShowing)
    8.             yield return null;
    9.         //this got it working
    10.         UnityEditor.EditorApplication.isPlaying = false;
    11.  
    12.         Time.timeScale = currentTimeScale;
    13.     }
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
  7. gilmour

    gilmour

    Joined:
    Aug 16, 2015
    Posts:
    6
    yeah thanks ill do that
     
  8. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    File those under" ways to piss off players one last time when they try to close my game"
     
    Oribow and Kiwasi like this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  10. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    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.
     
  11. gilmour

    gilmour

    Joined:
    Aug 16, 2015
    Posts:
    6
    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