- Home /
Will Unity wait the method to finish before the application is closed/quit/stop?
I have written a script which will write log file for a period of time or when some conditions are met by using StreamWriter class. I want to ask if by any chance that the application is quit, exit or closed by user (not crash) while it is still writing log file. Will Unity stop immediately or will it wait the method to finish writing log file first and then quit? In other word, I want to know if there is a chance that the script will stop writing log file halfway when the unity is quit or not. Note that I've worked in editor mode. Quit means press the stop button.
Answer by Cyb3rManiak · Aug 22, 2010 at 12:01 PM
It really depends on how you write your code. There is always a chance Unity will quit in the middle of the writing of the log, for example, if it's done as a loop. Just like a game has some kind of game loop - you can still quit a game whenever you want.
You can use the OnDisable() and OnApplicationQuit() callback methods to perform some cleanup and to check the status of your log files (close the StreamWriter, flush bufferes or whatever).
You can use Application.CancelQuit() to make sure Unity doesn't close during those operations. Check out the example in the link for some ideas on how to implement it.
Your answer
Follow this Question
Related Questions
onExit, onQuit, or anything like that? 2 Answers
Application.Quit() not working 2 Answers
Delay quitting and finish some coroutines 1 Answer
Unity will not open(Linux) 0 Answers