Is there a way to make mass Gameobject Instantiation smoother in unity 5.4+

Edit:

Ive now realized that my previous question was way to complicated to comprehend, hense i am now rewriting it all to focus on my questions and nothing else.

Hope this will make more sence:

The problem:
So im building a game with some massive Instantiation. About 200-400 GameObejcts need to be Instantiated on start. There is no other way, it must be done at some point, and no way to “load more of them later” or similar. This is a very heavy operation, that will freeze the game for several seconds or even kill unity.

My Question:
Is there a way in Unity 5.4+ to avoid freezing up the main thread and opening it up for a few milliseconds to allow other processes to run for a short while, and then continue doing the Instantiations again ?

Example:
This is some “fictional code”.

csharp** **foreach (var obj in MyObjects) { // do Instantiation stuff ..... // and then pause this loop or Coroutine for 0.05f seconds. yield Timeout (0.05f); // or something. }** **

Could any of this be done. or is there a new built in Unity 5.4+ feature that does this kind of threading of sorts?
I have already tried using “coroutines” and doing my own threading wont work since im doing alot of Monobehavior stuff in the function that im trying to fix.

Only compute chunks that the player can see. In Minecraft you can only see, what, a few hundred blocks into the distance? Compute others as necessary when the player moves far enough.

–Eric

What Eric5h5 said.

To be clear on what MineCraft does though. As you might know in Minecraft you can set a render distance in chunks. That setting controls how many chunk in all directions from your current chunk gets rendered. For example, a render distance of 10 would generate 10 chunks in all direction meaning you can see 160 blocks into the distance (a chunk in minecraft is 16x16x256) in all directions (well except diagonal of course).

Sorry guys, I just realized that a very important piece of information was forgotten… i added an “edit” to the description.

Hmm, then you can still render only those chunks that are in your FOV though, the same concept still is valid, though you’ll need to use zoom level to specify the amount of chunks to render. Another thing you can do what Minecraft does is not render each block seperately but rather use so called display lists, or even make sure a chunk is just one mesh and update that mesh whenever something changes. Ton of information on the following links:
Mine craft chunks: Chunk – Minecraft Wiki
Diplay lists video (20m50s):
https://www.youtube.com/watch?v=dTFkmfnkCfk

I do exactly that, i used this tutorial series (modified to only use x and z, with the y being a height from a heightmap):
https://www.youtube.com/watch?v=5hCkhoY5EyM
…

My problem isnt Lag or Slowness ingame. Its the “Instantiation” part of the game. The “press play and then wait 10-20 seconds in a frozen state until all Instantiation is complete” part :slight_smile:

I imagined myself that the only solution is to gradually Instantiate One chunk at a time in a threadsafe thread (using something that unity has created for it), and then have the Main thread freed up to do the progress bar etc.
It might not exist or maybe the Instantiation process MUST run in the main unity thread for it to work, I dont know, that why im here :slight_smile:

I would show an UI so the player can know that the map is loading. For example just a simple loading bar should be nice :wink:

Well, then I would go for a situation where you only load what is in the viewport (and then some), and the dynamically load new chunk when people scroll around, that will cut the loading time quite a bit. What you’d need to do for that is get the current center point of the screen in world coords and then set a specific render (or loading rather) distance from that point where all chunk in between those positions will be loaded.