-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathTimedDestroySystem.cs
44 lines (36 loc) · 1 KB
/
TimedDestroySystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Unity.Entities;
using Unity.Jobs;
namespace ECS.Systems
{
[UpdateAfter(typeof(MoveForwardSystem))]
public class TimedDestroySystem : JobComponentSystem
{
EndSimulationEntityCommandBufferSystem buffer;
protected override void OnCreate()
{
buffer = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
struct CullingJob : IJobForEachWithEntity<TimeToLive>
{
public EntityCommandBuffer.Concurrent commands;
public float dt;
public void Execute(Entity entity, int jobIndex, ref TimeToLive timeToLive)
{
timeToLive.Value -= dt;
if (timeToLive.Value <= 0f)
commands.DestroyEntity(jobIndex, entity);
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var job = new CullingJob
{
commands = buffer.CreateCommandBuffer().ToConcurrent(),
dt = UnityEngine.Time.deltaTime
};
var handle = job.Schedule(this, inputDeps);
buffer.AddJobHandleForProducer(handle);
return handle;
}
}
}