The topic with the same title here
How to efficiently spawn & render many cube prefabs for a voxel world?
is actually asking about efficiently rendering.
Assume I need to instantiate a lot of game objects. Without using ECS, anyway to batch the instantiating?
The traditional codes I thought:
for (int i = 0; i < n; ++i)
{
Instantiate(prefab, parent);
}
compared with the ECS approach:
NativeArray<Entity> entitiesHolder =
entityMgr.Instantiate(prefab, n, Allocator.Persistent);
The for loop approach is very ill performance, while the ECS approach is extraonary good, beyond the imagination. But in my case, the currently working feature can't implement ECS.
So, in Unity traditional, are there anyway to batch the instantiating?
Updated
I am aware of Object Pooling, and implemented that already.
I have a UI panel, displays a grid of 1000 items. Only 84~94 items appear on a scroll panel at a time, so I made a Recycler View (a kind of Object Pooling) with only 100 items.
Although so, looping instantiating 100 items is still too much. When the player switches to other other feature (close the current UI panel), I need to destroy all the items, and when the player open the UI panel, I need to instantiate them again. The items can't be re-use for other features, and the reason I destroy all of them when closing the UI is saving memory.
For decent phones and computers, that is not much, but for android phones with the like Snapdragon 400s or MTK (28nm) chips, and while the ads (my income) consumes somewhere 50% of the phones's processing power and memory already, performance is critical. Countless of users in India and SounthEast Asia still use those low-end devices, I still want to bring them the best experience as I can.
I have experience working with ECS, it performs very smooth even on low-end devices. It is just that my current working features are very hard to be implement on ECS (not impossible). While instantiating just 100 UI items causes throttle in the phones, instantiating 10 000 entities at the same time feel like nothing.
But if I use for
10k loops to instantiate each entity, it doesn't as effective as the batch instantiating entityMgr.Instantiate(prefab, n, Allocator.Persistent)
, so I believe in the native codes, the Unity's development team had optimized it very well. Since I am also experienced in C++ and memory-swap management, these optimizations are expected.
So, for MonoBehaviour objects, is there a way to batch the instantiating (to gain benefits from the native layer codes) too? If there is no other choice, I guess I have to go with ECS.
-
1\$\begingroup\$ More information about the context can help us find solutions that will work for that case, even if they're not general enough to work universally. Can you tell us more about what these prefabs are / what they represent in your world? We might be able to find a lighter-weight way to serve that role. \$\endgroup\$DMGregory– DMGregory ♦2024年07月16日 11:58:27 +00:00Commented Jul 16, 2024 at 11:58
-
\$\begingroup\$ When you profile the code do you see a problem with all the instantiations or are some of them more impactful than others? Are there any tipping points (i.e. after 1k object performance drops)? \$\endgroup\$Pikalek– Pikalek2024年07月16日 19:03:41 +00:00Commented Jul 16, 2024 at 19:03
-
\$\begingroup\$ @DMGregory I am aware of many optimization methods. Just curious if there is any method to batch the instantiating of MonoBehaviour objects. For further optimization, like Object Pooling, I can research it in other topics. Thank you very much. \$\endgroup\$Suratraak– Suratraak2024年07月17日 05:08:45 +00:00Commented Jul 17, 2024 at 5:08
-
\$\begingroup\$ Can you tell us more about these items, or show us a sample? If they're just sprites, you could use a trick similar to the one I show here to render a large grid of sprites with only a single raw image object. You would no longer have individual game objects to instantiate for each — instead you'd do a little grid math to work out which one got clicked based on its coordinates, and look up the matching data entry in an array. This gets tougher if the objects are 3D or animated, but we may be able to get clever with it. \$\endgroup\$DMGregory– DMGregory ♦2024年07月17日 12:31:01 +00:00Commented Jul 17, 2024 at 12:31
-
\$\begingroup\$ @DMGregory I had made a color palette table before, with only an image, fake displaying it as a grid. When clicking on a (fake) cell, I calculated the clicking position and overdraw a highlighter on the (fake) cell like it is being highlighted. I'd forgotten about it in this case. Thank you very much. \$\endgroup\$Suratraak– Suratraak2024年07月18日 07:39:01 +00:00Commented Jul 18, 2024 at 7:39
You must log in to answer this question.
Explore related questions
See similar questions with these tags.