My app relies on the Quit event to call the End() function in order to stop working properly, otherwise unity will crash.
But since this is not called inside of editor is there any other way to detect this event, other than creating a dummy button on the screen? Maybe there’s an editor script?
I think the last live training used something like this
using UnityEngine;
using System.Collections;
public class QuitApplication : MonoBehaviour {
public void Quit()
{
//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
}
}
4 Likes
@herman111 That’s a function that you can call from a button and it will end your app.
This is exactly what I said I want to avoid.
I’m looking for an alternative to OnApplicationQuit()
Have you tried putting some object in your scene that will never, under normal circumstances, be deactivated or destroyed — and then triggering your clean-up code from OnDestroy (or OnDisable)?
What do you mean it’s not called inside the editor?
::runs test on unity 4.6.7f1::
Yeah… just checked, OnApplicationQuit ran when I quit play in the editor.
Turns out my test just wasn’t showing that the OnApplicationQuit is called.
Btw JoeStrout good idea about using OnDestroy/OnDisable events.
1 Like
This works in both. The previous code similar to this does not work.
#if UNITY_EDITOR
// Application.Quit() does not work in the editor so
// UnityEditor.EditorApplication.isPlaying need to be set to false to end the game
UnityEditor.EditorApplication.isPlaying = false;
#endif
Application.Quit();
You just necro’d a 5-year-old thread to repeat the incorrect solution in the second post (missing the point of the question).
This is the correct way to quit an app in both editor and build, but it’s not what the OP was asking (but he already got the answer he needed on Jul 30, 2015).
4 Likes