How to handle actual process exit?
I'm making a game that uses a native plugin for Windows that must have its shutdown function called when the application exits. The plugin uses an independent worker thread to process a queue, and if that thread doesn't get told to stop, the process will not terminate until the user kills it through the task manager or similar.
Currently, I'm calling the shutdown function from Unity's Application.quitting event. This works fine for a standalone player, but it's unsatisfactory in the Unity editor, because Application.quitting is invoked when the editor leaves play mode, not when the editor is closed. So if you try to close the editor while it's in play mode, shutdown function doesn't get called and the editor becomes unresponsive but doesn't terminate.
There has got to be a better way, something that lets me run code when the actual application is actually closing. Searching the web has only pointed me at functionality in System.Windows.Forms, which doesn't seem to be supported in Unity.
Answer by muckenhoupt · Jul 25, 2020 at 09:14 PM
Figured it out. The key is that EditorApplication also has a quitting event. All I have to do is:
#if UNITY_EDITOR
EditorApplication.quitting += OnQuit;
#else
Application.quitting += OnQuit;
#endif
Your answer
Follow this Question
Related Questions
Check When Game Window is Closed for Windows 0 Answers
How to add "Press ESC to EXIT"? and exit the game? 1 Answer
Gray screen when trying to open unity. 7 Answers
Can I run Unity 4.7 on windows xp 1 Answer
UNITY 5 GREY SCREEN HELP! 1 Answer