- Home /
Prevent Quit (Windows)
Is there a way to respond to (and perhaps cancel) a quit event when the user clicks the X button in the top right corner of a Unity-generated window? I want to ask "Are you sure you want to quit?" in order to prevent users from accidentally killing the game while clicking around.
Thanks for your time!
Answer by Bunny83 · Feb 08, 2012 at 02:40 AM
Just look for Application.CancelQuit and MonoBehaviour.OnApplicationQuit. Note: This will only work in standalone apps. iPhone or Webplayer just ignore this (at least CancelQuit).
@davidjsushil: did this work? I'm trying to confirm quiting to give the user a chance to save some things, but usually when OnApplicationQuit() is called, OnDestroy() already got rid of most objects I'd like to save.
Answer by xylax · Feb 08, 2012 at 02:11 AM
First you would need pro to call native OS code Link. After that it would look somthing like this:
Application.Exit( );
if (MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
Xylax, thanks for the response. I don't have a pro license (yet), so I can't explore the solution you suggested. I was hoping that perhaps Unity had built-in support for handling a quit event - a method that could be overridden or something. So, am I out of luck for now, then?
You could try locking the mouse inside the frame (http://forum.unity3d.com/threads/3427-Constrain-$$anonymous$$ouse-Cusor-While-Fullscreen-(Windows))
I don't see any need for a native code plugin and how you could intercept the quit - message "legally". The only way from a native code plugin to intercept the close message is to subclass the Unity window (which is more hacking than game-developing). Also what's Application.Exit? Are you thinking of a C# windows application? In Unity there's just an Application.Quit
@davidjsushil: Please search the docs first next time. Since all scripts / classes that can receive callbacks from Unity have to be derived from $$anonymous$$onoBehaviour, that should be the first thing to look up. Almost all possible callbacks are listed there.
Bunny, I wouldn't have posted without searching for a solution first. Thanks, though!
Your answer
Follow this Question
Related Questions
Cancel a build process (Windows, Unity 5) 1 Answer
Unity freezes when closed, reinstalling doesn't help 0 Answers
My Mac Build doesn't work when I try to open it 1 Answer
I use Application.wantsToQuit Prevent success,but my websocket connection will be disconnected.why?, 0 Answers
Problem quitting app on Win7 64bit 1 Answer