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);
}
}
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.
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.