Skip to main content
more details
Source Link

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.

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?

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.

Rollback to Revision 2
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

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?

Thank you.

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?

Thank you.

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?

added 14 characters in body
Source Link

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?

Thank you.

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?

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?

Thank you.

added tag
Link
Loading
Source Link
Loading