I’m trying to make a system in ECS, that will show an outline around objects. My plan is to show this with two additional materials (mask and fill) for an entity. My starting point is the asset “Quick Outline”
As far as I have understood I have to make new entities to show additional materials for an entity so the way I have implemented it is to have a component like this:
public struct OutlineComponent : IComponentData
{
public Entity MaskEntity;
public Entity FillEntity;
public bool Show;
}
Then I have a system where I do this in OnUpdate:
var ecb = this.ecbSystem.CreateCommandBuffer();
Entities
.WithoutBurst()
.ForEach((Entity entity, int entityInQueryIndex, ref OutlineComponent outline, in RenderMesh renderMesh, in RenderBounds renderBounds) =>
{
if (outline.Show && outline.FillEntity == Entity.Null)
{
outline.FillEntity = CreateChildEntity(entity, renderMesh, archetype, materialFill, ref ecb);
}
else if (!outline.Show && outline.FillEntity != Entity.Null)
{
ecb.DestroyEntity(outline.FillEntity);
outline.FillEntity = Entity.Null;
}
if (outline.Show && outline.MaskEntity == Entity.Null)
{
outline.MaskEntity = CreateChildEntity(entity, renderMesh, archetype, materialMask, ref ecb);
}
else if (!outline.Show && outline.MaskEntity != Entity.Null)
{
ecb.DestroyEntity(outline.MaskEntity);
outline.MaskEntity = Entity.Null;
}
}).Run();
Where this.ecbSystem is EndSimulationEntityCommandBufferSystem
CreateChildEntity(…) calls ecb.CreateEntity() and adds components
Flipping the OutlineComponent.Show bool to true works and the outline is displayed, but when set to false I get this error:
ArgumentException: System.InvalidOperationException: playbackState.CreateEntityBatch passed to SelectEntity is null
Anyone who can help me to solve this issue?
Would also welcome all feedback on the approach in general, since I’m new to ECS.
EDIT
Everything works if I use EntityQuery, a normal foreach loop and the EntityManager directly instead of the command buffer.