forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLifeTimeSystem.cs
More file actions
58 lines (46 loc) · 1.51 KB
/
Copy pathLifeTimeSystem.cs
File metadata and controls
58 lines (46 loc) · 1.51 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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<EndSimulationEntityCommandBufferSystem>();
}
// Use the [BurstCompile] attribute to compile a job with Burst.
// You may see significant speed ups, so try it!
[BurstCompile]
struct LifeTimeJob : IJobForEachWithEntity<LifeTime>
{
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;
}
}