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!

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