protected override JobHandle OnUpdate(JobHandle inputDeps) { JobHandle jobHandle; // Increment the frame count on all active TriggerVolume components JobHandle updateVolumesJobHandle = new UpdateTriggerVolumesJob().Schedule(this, inputDeps); // Get the number of TriggerEvents so that we can allocate a native array m_TriggerEntitiesIndex[0] = 0; JobHandle getTriggerEventCountJobHandle = new GetTriggerEventCount() { pCounter = m_TriggerEntitiesIndex, }.Schedule(m_StepPhysicsWorldSystem.Simulation, ref m_BuildPhysicsWorldSystem.PhysicsWorld, inputDeps); getTriggerEventCountJobHandle.Complete(); // Get the list of overlapping bodies var triggerEntities = new NativeArray <TriggerEntities>(m_TriggerEntitiesIndex[0], Allocator.TempJob); m_TriggerEntitiesIndex[0] = 0; JobHandle listTriggerEventEntitiesJobHandle = new ListTriggerEventEntitiesJob { PhysicsVelocityGroup = GetComponentDataFromEntity <PhysicsVelocity>(true), VolumeGroup = GetComponentDataFromEntity <TriggerVolume>(true), TriggerEntities = triggerEntities, pCounter = m_TriggerEntitiesIndex, }.Schedule(m_StepPhysicsWorldSystem.Simulation, ref m_BuildPhysicsWorldSystem.PhysicsWorld, updateVolumesJobHandle); // Increment the frame count on the OverlappingTriggerVolume components // that are still present in the TriggerEvent list var overlappingGroup = GetComponentDataFromEntity <OverlappingTriggerVolume>(); JobHandle updateOverlappingJobHandle = new UpdateOverlappingJob { TriggerEntites = triggerEntities, TriggerEntitiesCount = m_TriggerEntitiesIndex, OverlappingGroup = overlappingGroup, }.Schedule(listTriggerEventEntitiesJobHandle); // Add OverlappingTriggerVolume components to any Entity that has // just entered a trigger volume var triggerGroup = GetComponentDataFromEntity <TriggerVolume>(true); JobHandle addNewJobHandle = new AddNewOverlappingJob { CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer(), TriggerEntities = triggerEntities, TriggerEntitiesCount = m_TriggerEntitiesIndex, TriggerGroup = triggerGroup, OverlappingGroup = overlappingGroup, }.Schedule(updateOverlappingJobHandle); m_EntityCommandBufferSystem.AddJobHandleForProducer(addNewJobHandle); // Remove the OverlappingTriggerVolume component from any Entity // that did not have its frame count incremented and so will be behind // the frame count of the associated trigger. i.e. it has left the trigger volume JobHandle removeOldJobHandle = new RemoveOldOverlappingJob { CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer(), OverlappingEntities = OverlappingGroup.ToEntityArray(Allocator.TempJob), TriggerGroup = triggerGroup, OverlappingGroup = overlappingGroup, }.Schedule(updateOverlappingJobHandle); m_EntityCommandBufferSystem.AddJobHandleForProducer(removeOldJobHandle); jobHandle = JobHandle.CombineDependencies(addNewJobHandle, removeOldJobHandle); return(jobHandle); }
protected override void OnUpdate() { // Get the number of TriggerEvents so that we can allocate a native array m_TriggerEntitiesIndex[0] = 0; JobHandle getTriggerEventCountJobHandle = new GetTriggerEventCount { NumTriggerEvents = m_TriggerEntitiesIndex, }.Schedule(m_StepPhysicsWorldSystem.Simulation, ref m_BuildPhysicsWorldSystem.PhysicsWorld, Dependency); getTriggerEventCountJobHandle.Complete(); // Increment the frame count for all TriggerVolumes // This frame count is compared with the overlapping entity frame count so that we can tell if an entity has left the volume. JobHandle updateVolumesJobHandle = Entities .WithName("UpdateTriggerVolumeFrameCounts") .WithBurst() .ForEach((ref TriggerVolume triggerVolume) => triggerVolume.CurrentFrame++).ScheduleParallel(Dependency); // Get the list of overlapping bodies var triggerEntities = new NativeArray <TriggerEntities>(m_TriggerEntitiesIndex[0], Allocator.TempJob); m_TriggerEntitiesIndex[0] = 0; JobHandle listTriggerEventEntitiesJobHandle = new ListTriggerEventEntitiesJob { PhysicsVelocityGroup = GetComponentDataFromEntity <PhysicsVelocity>(true), VolumeGroup = GetComponentDataFromEntity <TriggerVolume>(true), TriggerEntities = triggerEntities, NumTriggerEntities = m_TriggerEntitiesIndex, }.Schedule(m_StepPhysicsWorldSystem.Simulation, ref m_BuildPhysicsWorldSystem.PhysicsWorld, updateVolumesJobHandle); // Increment the frame count on the OverlappingTriggerVolume components // that are still present in the TriggerEvent list var overlappingGroup = GetComponentDataFromEntity <OverlappingTriggerVolume>(); JobHandle updateOverlappingJobHandle = new UpdateOverlappingJob { TriggerEntites = triggerEntities, TriggerEntitiesCount = m_TriggerEntitiesIndex, OverlappingGroup = overlappingGroup, }.Schedule(listTriggerEventEntitiesJobHandle); // Add OverlappingTriggerVolume components to any Entity that has // just entered a trigger volume var triggerGroup = GetComponentDataFromEntity <TriggerVolume>(true); JobHandle addNewJobHandle = new AddNewOverlappingJob { CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer(), TriggerEntities = triggerEntities, TriggerEntitiesCount = m_TriggerEntitiesIndex, TriggerGroup = triggerGroup, OverlappingGroup = overlappingGroup, }.Schedule(updateOverlappingJobHandle); m_EntityCommandBufferSystem.AddJobHandleForProducer(addNewJobHandle); // Remove the OverlappingTriggerVolume component from any Entity // that did not have its frame count incremented and so will be behind // the frame count of the associated trigger. i.e. it has left the trigger volume JobHandle removeOldJobHandle = new RemoveOldOverlappingJob { CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer(), OverlappingEntities = OverlappingGroup.ToEntityArray(Allocator.TempJob), TriggerGroup = triggerGroup, OverlappingGroup = overlappingGroup, }.Schedule(updateOverlappingJobHandle); m_EntityCommandBufferSystem.AddJobHandleForProducer(removeOldJobHandle); Dependency = JobHandle.CombineDependencies(addNewJobHandle, removeOldJobHandle); }