using Unity.Burst; using Unity.Collections; using Unity.Entities; using Unity.Jobs; public struct LifeTime : IComponentData { public float Value; } // This system updates all entities in the scene with both a RotationSpeed_SpawnAndRemove and Rotation component. public class LifeTimeSystem : JobComponentSystem { EntityCommandBufferSystem m_Barrier; protected override void OnCreate() { m_Barrier = World.GetOrCreateSystem(); } // Use the [BurstCompile] attribute to compile a job with Burst. // You may see significant speed ups, so try it! [BurstCompile] struct LifeTimeJob : IJobForEachWithEntity { public float DeltaTime; [WriteOnly] public EntityCommandBuffer.Concurrent CommandBuffer; public void Execute(Entity entity, int jobIndex, ref LifeTime lifeTime) { lifeTime.Value -= DeltaTime; if (lifeTime.Value < 0.0f) { CommandBuffer.DestroyEntity(jobIndex, entity); } } } // OnUpdate runs on the main thread. protected override JobHandle OnUpdate(JobHandle inputDependencies) { var commandBuffer = m_Barrier.CreateCommandBuffer().ToConcurrent(); var job = new LifeTimeJob { DeltaTime = Time.DeltaTime, CommandBuffer = commandBuffer, }.Schedule(this, inputDependencies); m_Barrier.AddJobHandleForProducer(job); return job; } }