Skip to content

Track addition or removal

Declare Reactive Component

public struct ExampleComponent : IReactiveAddOrRemoveComponent {}
  • Must derive from IReactiveAddOrRemoveComponent.
  • Can be class or struct, can derive from IEnableableComponent.
  • Must have the corresponding Shadow Component defined.

Declare Shadow Component

[TrackAddOrRemove(typeof(ExampleComponent))]
public partial struct ExampleComponentShadow : IComponentData {}
  • Must be marked with TrackAddOrRemove attribute which indicates the corresponding Reactive Component.
  • Must be partial struct.
  • Must derive from IComponentData.
  • Must be properly named with following rule: reactive component name + "Shadow".
  • Must have the corresponding Reactive Component defined.

Make changes

Just add, remove, enable or disable the Reactive Component in whatever way you want.

commandBufferParallelWriter.SetComponentEnabled<ExampleComponent>(entityInQueryIndex, entity, false);
...
commandBuffer.RemoveComponent<ExampleComponent>(entity);
...
EntityManager.AddComponent<ExampleComponent>(entity);

Listen for changes

Detect component added or enabled

Create and execute a query in whatever way you want. Your query must ask for all entities with Reactive Component and with none of Shadow Component:

Dependency = Entities.WithAll<ExampleComponent>().WithNone<ExampleComponentShadow>()
   .ForEach((Entity entity) => { ... }).ScheduleParallel(Dependency);

Detect component removed or disabled

Create and execute a query in whatever way you want. Your query must ask for all entities with Shadow Component and with none of Reactive Component:

Dependency = Entities.WithNone<ExampleComponent>().WithAll<ExampleComponentShadow>()
   .ForEach((Entity entity) => { ... }).ScheduleParallel(Dependency);