Currently, I’m using Unity ECS to simulate a simple world. The world is a grid of about 1000 x 1000 cells. Each cell is treated as a separate unit, totaling 1 million units. Additionally, I’m simulating the movement and growth of plants and animals on this terrain. Each cell can only have one plant, indicated by an occupied
component on each cell entity.
Currently, when I need to spawn a new plant, I go through all 1 million cell entities to find the one corresponding to the plant’s position in the world. This process involves the following code:
static public Entity GetCellEntity(int2 position, EntityManager em)
{
EntityQuery query = new EntityQueryBuilder(Allocator.Temp).WithAll<Position>().Build(em);
var entityes = query.ToEntityArray(Allocator.Temp);
foreach(Entity entity in entityes)
{
Position pos = em.GetComponentData<Position>(entity);
if(pos.gridPosition.x == position.x && pos.gridPosition.y == position.y)
{
return entity;
}
}
entityes.Dispose();
query.Dispose();
return Entity.Null;
}
However, this method is highly inefficient and time-consuming. Do you have any suggestions for improving this function or restructuring the code, such as possibly having all cells under a single large entity or grouping entities as chunks of cells?