Best way for Threading terrain generation

Hello,
I have a custom terrain generator to make galaxy and system maps.
The generator will generate spots and then instantiate prefabs on that location.

Right now i have a delay of some seconds when i want to generate a map.
I like to do this in the background instead.

As example i think on this: Have the Player select the settings for a galaxy then press on generate have it generated in the background while the player selects his race.

I did read that Unity is itself not thread save.
What would the right way to approach this?

You will not be able to thread the actual Instantiate calls.

If there’s other work you do that’s complex, like if the algorithm for selecting positions does more than just randomly select a spot. Maybe it selects spots that are aesthetically pleasing or something…

Well that part can be threaded. In your thread you fill up an array/list with a struct of some sort that represents the data needed for each instantiate (the prefab, the position, the rotation, etc).

While this goes on you can show the race select screen or whatever.

Then when you go to actually start the race (I presume), you loop over said array/list and instantiate everything.

Is there a way to tell the function that makes the maps to go back to the screen / menu so the menu will still be responsive while the generator is working?
Maybe alternatively i could run the generator with update, run parts of the generator save all and continue with the next update…

I tried a bit around the gameobjects are the really heavy stuff all other is rather inexpensive.

Ultimately i made it so the generator is started from update and i handle everything else myself, it works well enough and feels much better than any unity side solution…