- Home /
Mass object instantiation issue.
Imagine you have a mesh of only 5 or 6 polygons, and you need to instantiate a whole bunch of them. maybe 100. It's like you press hold the mouse button and will instantiate using a timer. every 0.05 sec. So I noticed that the performance falls dramatically( Draw call arises rapidly) . I'm kinda surprised here since unity is able to handle way more polygons, on each frame. So my guess is that objects that are instantiated on runtime are dealt with differently than ones already there before the level starts. i need to add that after instantiation they are static objects. So what's the best solution out there?
Edit: these small objects are sharing the same material however on the moment they are instantiated i give them a random color.
Cheers,
What do the objects contain? (ie,rigidbodies, colliders etc) chances are the performance falls due to the fact you are instantiating so many objects, once it's caught up with itself and stopped instantiating how does it run?
it's still slow even when stopped. I check it with profiler.
Answer by Eric5h5 · Apr 08, 2011 at 08:48 PM
I'd recommend using the Mesh class to construct a single mesh made from the polygons, rather than having a separate object for each one.
you mean after the mouse is up, i combine them into a single mesh? sounds good, however if we want to remove few of those object, then we can't.
No, I mean construct the mesh continuously as the mouse is held down by adding polygons as necessary. You can remove some of the objects by rebuilding the mesh $$anonymous$$us the appropriate polygons.
Yeah, you'll probably get sky high draw call count with separate objects. $$anonymous$$aking a mesh out of it would reduce the draw calls significantly (to 1, from 100s). A down side is that you can't put individual scripts on these objects any longer, so you have to rethink any possible behaviour these individual objects would have had. But it's doable, and you'll combat the frame drop. +1
There reason I can't really combine these objects is that, users need to be able to remove any number of these objects at any point in time.
@$$anonymous$$ourosh: you can still combine them, it's just a little more complicated. See here for something related: http://forum.unity3d.com/threads/4477-in-and-out-of-a-combined-mesh
Answer by PrimeDerektive · Apr 08, 2011 at 08:03 PM
try rolling your own recycling system.
Decide what you think the maximum amount of objects of that type you think will ever have at any one time will be, and instantiate (and disable) that many and add them into an array in Start().
Then, whenever you need one, iterate the array, enable the next object in the array, place it wherever you want it "instantiated" so forth, then when you're done, disable it again.
See this answer for more info.
What if i put those objects somewhere in the scene, around 100 of them. So that they are there from the beginning of the level. Just relocate them when mouse clicked? I mean would static feature (the checkbox that renders all static objects) work here?