forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelfDestructSystem.cs
More file actions
28 lines (25 loc) · 896 Bytes
/
Copy pathSelfDestructSystem.cs
File metadata and controls
28 lines (25 loc) · 896 Bytes
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
using Unity.Entities;
public struct SelfDestruct : IComponentData
{
public float TimeToLive;
}
[RequireMatchingQueriesForUpdate]
public partial class SelfDestructSystem : SystemBase
{
private EndSimulationEntityCommandBufferSystem m_EndSimECBSystem;
protected override void OnCreate()
{
m_EndSimECBSystem = World.GetExistingSystemManaged<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = m_EndSimECBSystem.CreateCommandBuffer().AsParallelWriter();
var dt = SystemAPI.Time.DeltaTime;
Entities.ForEach((Entity entity, int entityInQueryIndex, ref SelfDestruct spawner) =>
{
if((spawner.TimeToLive -= dt) < 0)
ecb.DestroyEntity(entityInQueryIndex, entity);
}).ScheduleParallel();
m_EndSimECBSystem.AddJobHandleForProducer(Dependency);
}
}