Disabling a hierarchy of entities

Just wondering: is there already an easy way to disable a hierarchy of entities by just placing some component on the parent entity, or is iterating on all child entities and adding the Disabled component the only way?

EDIT: just found out about EntityManager.SetEnabled with a LinkedEntityGroup, but is there a job-compatible equivalent for EntityCommandBuffer?

1 Like

FYI. you can use EntityManage in bursted job.
https://discussions.unity.com/t/811616
But it’s a structural change. so synpoint is needed.

Hope EntityCommandBuffer has SetEnable too.

For ECB specifically? No there is not. Depending on what your other requirements are and why you are asking, there might be other options. I’m actually working on creating specialized variants of ECB that use batch API on playback right now because ECBs are a bottleneck for me at scale.

in the meantime I simply made this util

public static void SetEntityHierarchyEnabled(bool enabled, Entity parent, EntityCommandBuffer.ParallelWriter commandBuffer, int chunkIndex, BufferFromEntity<LinkedEntityGroup> linkedEntityGroupFromEntity)
{
    if (enabled)
    {
        commandBuffer.RemoveComponent<Disabled>(chunkIndex, parent);
    }
    else
    {
        commandBuffer.AddComponent<Disabled>(chunkIndex, parent);
    }

    if (linkedEntityGroupFromEntity.HasComponent(parent))
    {
        DynamicBuffer<LinkedEntityGroup> parentLinkedEntities = linkedEntityGroupFromEntity[parent];
        if (enabled)
        {
            for (int i = 0; i < parentLinkedEntities.Length; i++)
            {
                commandBuffer.RemoveComponent<Disabled>(chunkIndex, parentLinkedEntities[i].Value);
            }
        }
        else
        {
            for (int i = 0; i < parentLinkedEntities.Length; i++)
            {
                commandBuffer.AddComponent<Disabled>(chunkIndex, parentLinkedEntities[i].Value);
            }
        }
    }
}

LinkedEntityGroup[0] is the parent itself.
so if there is a LinkedEntityGroup, you just need a for loop. don’t need to process parent specifically.

2 Likes
    public static class EcbExt
    {
        public static void SetEntityHierarchyEnabled(
            this ref EntityCommandBuffer.ParallelWriter ecbp, int sortID, Entity entity, bool enabled,
             BufferFromEntity<LinkedEntityGroup> linkedEntityGroupFromEntity)
        {
            if (linkedEntityGroupFromEntity.HasComponent(entity)) ecbp.SetEntityHierarchyEnabled(sortID, entity, enabled, linkedEntityGroupFromEntity[entity]);
            else
            {
                if (enabled) ecbp.RemoveComponent<Disabled>(sortID, entity);
                else ecbp.AddComponent<Disabled>(sortID, entity);

            }
        }

        //for IJobChunk that you can test LinkedEntityGroup existence at chunk level;
        public static void SetEntityHierarchyEnabled(
            this ref EntityCommandBuffer.ParallelWriter ecbp, int sortID, Entity entity, bool enabled,
             DynamicBuffer<LinkedEntityGroup> buffer)
        {
            if (enabled)
            {
                for (int i = 0, end = buffer.Length; i < end; i++)
                    ecbp.RemoveComponent<Disabled>(sortID, buffer[i].Value);
            }
            else
            {
                for (int i = 0, end = buffer.Length; i < end; i++)
                    ecbp.AddComponent<Disabled>(sortID, buffer[i].Value);
            }
        }
    }

how about this one?

2 Likes

added none parallel ECB support

6395546–713138–EntityCommandBufferExt.cs (5.47 KB)

1 Like