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. Dismiss Notice

Feedback Feature request: Stop play-in-editor on Application.Quit

Discussion in 'General Discussion' started by Marscaleb, Feb 27, 2022.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    987
    It's kind of annoying that I can't actually test that the "quit" option is working in my game unless I make a complete build.
    I haven't actually had many problems because of this, but it still is something annoying me.

    You know what would be a reasonable thing to happen? How about my test play stops? Just the same as if I clicked the "play" button again.

    Or maybe a pop-up window? Well that might be annoying. But still, something to show that the game actually would try to quit at this point.
     
    AdamBebkoFV likes this.
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,848
    Let me introduce you to preprocessors:
    Code (CSharp):
    1.         public void QuitGame()
    2.         {
    3. #if UNITY_EDITOR
    4.             EditorApplication.ExitPlaymode();
    5. #else
    6.             Application.Quit();
    7. #endif
    8.         }
     
    neginfinity, TonyLi, Ryiah and 2 others like this.
  3. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    324
    I think it would be much cooler if
    Application.Quit()
    closed the Unity Editor entirely.
    I need more crash-esque Unity Editor functionality in my life, and this would do just that.

    In all seriousness though, I see no reason at all why
    Application.Quit()
    does not stop playmode by default.
     
    Chris9465 and MoonMoritz like this.
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    NotaNaN and spiney199 like this.
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    987
    I'm not too fond of adding the editor namespace to a script just for this.
    It's a valid idea and lets me see what this would be like, though.

    Honestly I still think it should just be built in to the editor. Maybe make it an option?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,848
    It doesn't require the editor name space.

    In any case neginfinity's answer is better than mine.
     
  7. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    It does.

    EditorApplication is declared within UnityEditor.
     
    MadeFromPolygons likes this.
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,848
    I shall tick off my Monday morning brain fart then. My brain thought the processors were being referred to there.
     
  9. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    You still don't need to import it, you can simply use the fully classified name
    Code (csharp):
    1. UnityEditor.EditorApplication.ExitPlaymode();
     
    Marscaleb and spiney199 like this.
  10. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    987
    So hey! It turns out that if you have that ExitPlayMode code in your script, you can't create a build! It counts as an error, even though it works fine within the editor.

    So yeah, a formal addition to the editor is still requested.
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    Did you have the conditional compilation flag around it? Because without it the build will try to reference the Unity Editor namespace, which it can't.
     
  12. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    987
    I did, in fact. The code should have been considered unreachable.
     
  13. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,848
    Did you put the conditionals around the namespace as well?
     
    angrypenguin likes this.
  14. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    Unreachable code is still compiled, and compilability is the issue at hand.
     
  15. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    On somewhat unrelated note, one of the amusing quirks of C# in comparison to C++, is that dead code that is never used can affect executable side. First, there's metadata (reflection data and attributes), which can grow quite large, second is that reflection you can't with certainty determine that something is never called. In comparison, C++ compiler and linker usually aggressively removes every unused function...