forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLifeTimeAuthoring.cs
More file actions
49 lines (43 loc) · 1.28 KB
/
Copy pathLifeTimeAuthoring.cs
File metadata and controls
49 lines (43 loc) · 1.28 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
using Unity.Collections;
using Unity.Entities;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;
public struct LifeTime : IComponentData
{
public int Value;
}
public class LifeTimeAuthoring : MonoBehaviour
{
[Tooltip("The number of frames until the entity should be destroyed.")]
public int Value;
}
public class LifeTimeBaker : Baker<LifeTimeAuthoring>
{
public override void Bake(LifeTimeAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity, new LifeTime { Value = authoring.Value });
}
}
[RequireMatchingQueriesForUpdate]
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(PhysicsSystemGroup))]
public partial struct LifeTimeSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
using (var commandBuffer = new EntityCommandBuffer(Allocator.TempJob))
{
foreach (var(timer, entity) in SystemAPI.Query<RefRW<LifeTime>>().WithEntityAccess())
{
timer.ValueRW.Value -= 1;
if (timer.ValueRW.Value < 0f)
{
commandBuffer.DestroyEntity(entity);
}
}
commandBuffer.Playback(state.EntityManager);
}
}
}