Spread heavy computation over multiple frames

Hi!

I have a map object that use an heavy computational initializing function like this

void Start () {
    LoadMapFromFile();
    ExtractFeaturesFromMap();
    DoSomeOtherHeavyStuff();
}

My desire is to spread the computation over multiple frames. In this way I can show something during the computation (partial outcomes of the algorithms, a progress bar or anything else).

For other didactical algorithms I had used a coroutine with WaitForEndOfFrame at the end of each top-level iteration, but it obviously waste too much time in a real application.

There is a way to perform this computation in an asynchronous way?

Thank you in advance!

What I did was just insert a yield WaitForSeconds(x) in there to spread the load. Might be a bit too simple for what you want, though.

Either you work with coroutines and place a yield aftr a computation block or you use threading to get access to the asynchronous way. Have a look into my thread helper asset which can make it a little bit easier for you, depending on what you are doing.

Thank you for the answers! :slight_smile:

For sure, using yield is the simplest way but I have some concerns on the overall performance. I have to find good spots where to put the yield instructions and hope for the best.

I’ll definitely try with the asynchronous way.