How to destroy Entity with its child Entities.

‘EntityManager.Destroy()’ or ‘EntityCommandBuffer.Destroy()’ destroys Entities corresponding to ‘EntityQuery’ or ‘EntityInQueryIndex’.

But they don’t destroy Child Entities.

I want to destroy certain Entities and their child Entities.

The code-generated Entities have ‘LinkedEntityGroup’ and ‘Child’ components.

Entities in the hierarchy have only ‘Child’ components.
They don’t have a child of child Entity.

In other words, entities with children do not unconditionally have a ‘Linked Entity Group’ component.

I want to destroy certain Entities with their all child Entities.
Please give me your advice.

If you add this mono behaviour to your ‘in scene gameobjects’ that will be converted. It should add the linkedEntitygroup which will allow all Children to get auto destroyed. Similar behaviour to Code instantiated prefabs.

using Unity.Entities;
using UnityEngine;


public class AddLinkedEntitiesConverter : MonoBehaviour, IConvertGameObjectToEntity
{
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        conversionSystem.DeclareLinkedEntityGroup(this.gameObject);
    }
}
2 Likes

Make a reactive system. If an Entity has a children buffer but no linked entities, add the linked entities buffer and populate it with all the children.

I don’t know how to proceed in the same way as your answer in SystemBase class.

Adding a LinkedEntityGroup buffer in SystemBase does not automatically add all children.
That means your opinion is not the answer to bringing all child entities.

Arnold_2013’s comment is not added by SystemBase, but it’s one of the answers to my questions.

If possible, its probably better to add the LinkedEntityGroup in conversion like what @Arnold_2013 mentioned. Also, depending on your setup, another option is to use IDeclareReferencedPrefabs like in this example: https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/ECSSamples/Assets/HelloCube/5.%20SpawnFromEntity/SpawnerAuthoring_FromEntity.cs

This prefab will automatically get a LinkedEntityGroup added and populated with all children in the same way as DeclareLinkedEntityGroup.

The other option is to add and populate the LinkedEntityGroup at runtime like @WAYNGames suggested. The reactive system they are referring to would look something like this:

public partial class LinkChildrenSystem : SystemBase {
   
    private EntityCommandBufferSystem _commandBufferSystem;
   
    protected override void OnCreate()
    {
        _commandBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
    }
   
    protected override void OnUpdate()
    {
        EntityCommandBuffer commandBuffer = _commandBufferSystem.CreateCommandBuffer();
       
        Entities.WithNone<LinkedEntityGroup>()
            .ForEach(
                (Entity entity, in DynamicBuffer<Child> children) =>
                {
                    DynamicBuffer<LinkedEntityGroup> group = commandBuffer.AddBuffer<LinkedEntityGroup>(entity);
                    group.Add(entity);  // Always add self as first member of group.
                    foreach (Child child in children)
                    {
                        group.Add(child.Value);
                    }
                })
            .Schedule();

        _commandBufferSystem.AddJobHandleForProducer(Dependency);
    }
}

This will find all entities with a Child buffer but no LinkedEntityGroup buffer, add the buffer and populated with self and all children. Depending on your use case, you may also want to include WithEntityQueryOptions(EntityQueryOptions.IncludePrefab) if you are instantiating prefabs at runtime. This will avoid most structural changes by ensuring that the prefabs have the correct LinkedEntityGroup before you instantiate from them.

2 Likes