CPU Intensive Function, how to call?
My game runs smoothly off of a very large set of data that I read in from a file. The issue is sometimes I need to read a new set of data in which takes anywhere from 1-10 seconds because this data set can be up to a GB of information.
Right now I am calling the function that updates the information in update, and when this happens the game obviously freezes for a little bit. Is there somewhere else that I can call this function that way the game still plays while the data is being updated?
Thank you for your help!
Up to a GB of information!? That seems extreme for any implementation. We would need more info on what type of data you are reading/writing, and when it is updated to be able to help.
Answer by Glurth · Oct 23, 2016 at 06:17 AM
You can use threading to launch the function that loads the data. The problem with using a separate thread is that it may not actually access any part of the Unity main thread, or classes. This could turn loading into a two part procedure, one to load from disk into your data structure, and a second part, (that does not run in a separate thread) to initialize unity with the loaded data.
Alternatively, you can use a counter in your load function, to stop loading, note the current progress and return
; this will allow unity's other tasks to proceed without hanging-up. Then during the next frame's update cycle you allow loading to continue, again, until the counter runs out. Repeat until loading is done.
You can adjust the max value of the counter to tune performance: a small counter will gave less impact per frame, but take longer to load (in total), a large counter will have more impact per frame, but load faster (in total).
Your answer

Follow this Question
Related Questions
Capture Screenshot on Linux Headless Machine 3 Answers
How many instatiates per frame is too much? 0 Answers
Get axe/arrow to stick to object 0 Answers
The editor got Massive FPS drop due to "Unaccounted time" says the profiler 0 Answers
Unity Stop/Freeze when Destroying+reloading a lot of GameObjects, solution ? 0 Answers