Using Object Pooling, assuming I'll like to spawn 500 cubes once every three seconds. How do I make it performance friendly? I need ideas, not the code itself.
-
\$\begingroup\$ Are the cubes suppose to move ? or are they completely static ? \$\endgroup\$– Coldsteel48Nov 10, 2019 at 9:28
-
1\$\begingroup\$ They are not static, their only job is to rotate and nothing else, and it is done on one line of code: transform.rotate(); \$\endgroup\$– GozmetaiemaxNov 10, 2019 at 9:30
-
2\$\begingroup\$ I think you should start with GPU instancing them - this will increase your overall performance - however for spawning (in this case pushing more transforms to GPU can still have some overhead) but you should try first if you already didnt \$\endgroup\$– Coldsteel48Nov 10, 2019 at 9:34
-
\$\begingroup\$ Okay, I'll use GPU instancing and see the results. \$\endgroup\$– GozmetaiemaxNov 10, 2019 at 9:37
-
\$\begingroup\$ Perhaps you will want to use some mixed approach : thats depends on the map - if you have a large map - then you will want to cluster the instanced actors (no idea what is the Unity term) for areas - because instancing means no occlusion checks - means every single instance will always be rendered. clusterizing by many instancing actors you can take some benefit of not rendering all of the cubes. \$\endgroup\$– Coldsteel48Nov 10, 2019 at 9:39
1 Answer
in unity basically pooling means: instead of using Instatntiate()
that makes new object in the scene that takes cpu performance to allocate, make all of needed object on the start of the scene and only enable and disable them and only make new object when all of you instantiated objects are active and are not ready to reuse.
you can use a foreach
or for
loop to look for in-active object to use it on a list but its not much performant. using a static array is much more performant than a list. as i searched on unity its best to use queue to make pooling. just en-queue an object that is ready to use and de-queue when it gets active.
just check this brackes link to check how its used:
-
1\$\begingroup\$ I know about this, and yes, I've seen Brackeys' YouTube video on Object Pooling and applied it, but I still have performance spikes on the frames that I enable an object. \$\endgroup\$ Nov 10, 2019 at 9:56
-
\$\begingroup\$ @MetaMax you are enabling many object in 3 seconds. its not a big deal but having more cpu usage in these moment dont thing is something weired \$\endgroup\$– virtousoNov 10, 2019 at 10:50