using Unity.Physics.Stateful;
//We did not need the ReferenceEntity so we deleted the IConvertGameObjectToEntity interface
//and the TriggerVolumeChangeMaterial component
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(StatefulTriggerEventBufferSystem))]
public partial class ChangeMaterialAndDestroySystem : SystemBase
private EndFixedStepSimulationEntityCommandBufferSystem m_CommandBufferSystem;
private EntityQueryMask m_NonTriggerMask;
protected override void OnCreate()
m_CommandBufferSystem = World.GetOrCreateSystem<EndFixedStepSimulationEntityCommandBufferSystem>();
m_NonTriggerMask = EntityManager.GetEntityQueryMask(
GetEntityQuery(new EntityQueryDesc
None = new ComponentType[]
typeof(StatefulTriggerEvent)
protected override void OnUpdate()
var commandBuffer = m_CommandBufferSystem.CreateCommandBuffer();
// Need this extra variable here so that it can
// be captured by Entities.ForEach loop below
var nonTriggerMask = m_NonTriggerMask;
.WithName("ChangeMaterialOnTriggerEnter")
.ForEach((Entity e, ref DynamicBuffer<StatefulTriggerEvent> triggerEventBuffer) =>
for (int i = 0; i < triggerEventBuffer.Length; i++)
var triggerEvent = triggerEventBuffer[i];
var otherEntity = triggerEvent.GetOtherEntity(e);
// exclude other triggers and processed events
if (triggerEvent.State == StatefulEventState.Stay || !nonTriggerMask.Matches(otherEntity))
if (triggerEvent.State == StatefulEventState.Enter)
var volumeRenderMesh = EntityManager.GetSharedComponentData<RenderMesh>(e);
var overlappingRenderMesh = EntityManager.GetSharedComponentData<RenderMesh>(otherEntity);
overlappingRenderMesh.material = volumeRenderMesh.material;
commandBuffer.SetSharedComponent(otherEntity, overlappingRenderMesh);
//The following is what happens on exit
commandBuffer.AddComponent(otherEntity, new DestroyTag {});
m_CommandBufferSystem.AddJobHandleForProducer(Dependency);