Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by davidjsushil · Feb 08, 2012 at 12:52 AM · windowsquitclosecancelprevent

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4

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).

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xylax · Feb 08, 2012 at 02:45 AM 0
Share

Thank you for correcting me.

avatar image davidjsushil · Feb 08, 2012 at 02:47 AM 0
Share

Fantastic! Thank you, Bunny. :-D

avatar image Radetic · Aug 04, 2015 at 09:10 PM 0
Share

@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.

avatar image
1

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();
 }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image davidjsushil · Feb 08, 2012 at 02:30 AM 0
Share

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?

avatar image xylax · Feb 08, 2012 at 02:36 AM 0
Share

You could try locking the mouse inside the frame (http://forum.unity3d.com/threads/3427-Constrain-$$anonymous$$ouse-Cusor-While-Fullscreen-(Windows))

avatar image Bunny83 · Feb 08, 2012 at 02:46 AM 0
Share

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

avatar image Bunny83 · Feb 08, 2012 at 02:48 AM 0
Share

@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.

avatar image davidjsushil · Feb 08, 2012 at 02:59 AM 0
Share

Bunny, I wouldn't have posted without searching for a solution first. Thanks, though!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges