Coroutine Yield no more often than necessary

So I’ve got a routine that instantiates ~40,000 gameobjects, mostly sprites. It takes about half a second to run on my development machine, pushing a single frame out over the duration. I’d like to use a coroutine to spread this process out over the appropriate number of frames. I can’t use a separate thread because it’s all unity engine operations.

However, Yield doesn’t really do what I want it to do. It yields, at minimum, to the next frame. Taking 40,000 frames to load the level would obviously push the load time to unacceptable lengths. Any place I care to put the yield statements - every 10,000, every 1000, whatever - could be tuned to a single environment, but would either lower the framerate or increase the load time anywhere slower or faster.

The other solution I was thinking of was to use a C# Stopwatch to Yield after a given amount of clock time, but again, it’s a bit of a guessing game to set that delay, and in my experience Stopwatches can be processor expensive themselves somehow.

Thank you for your attention.

if your instantiating that many that fast you probably want to look into object pooling, which is a technique designed to help with the overhead of instantiating too fast on mobile devices. Unity has a tutorial on that on the site

even better, can you get away with instantiating in groups and have behaviors on the parents of the groups? say if you have 50 groups each with 800 particles/objects moving by their parent. the reason I bring this up is because I shutter at what would happen if you try to put an update() on every single one of the 40,000 objects. if the groups are going to be inter-meshed no one should notice that a single scattered group is moving in unison when 49 other groups are mixed in distracting the player.

though if your simply having that many objects on the screen no one is going to notice any advanced individual behavior. so you might be better off using particles instead.