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
43 lines (37 loc) · 1.21 KB
/
Copy pathLifeTimeAuthoring.cs
File metadata and controls
43 lines (37 loc) · 1.21 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
using Unity.Collections;
using Unity.Entities;
using Unity.Physics.Systems;
using UnityEngine;
public struct LifeTime : IComponentData
{
public int Value;
}
public class LifeTimeAuthoring : MonoBehaviour, IConvertGameObjectToEntity
{
[Tooltip("The number of frames until the entity should be destroyed.")]
public int Value;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) =>
dstManager.AddComponentData(entity, new LifeTime { Value = Value });
}
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(BuildPhysicsWorld))]
public class LifeTimeSystem : SystemBase
{
protected override void OnUpdate()
{
using (var commandBuffer = new EntityCommandBuffer(Allocator.TempJob))
{
Entities
.WithName("DestroyExpiredLifeTime")
.ForEach((Entity entity, ref LifeTime timer) =>
{
timer.Value -= 1;
if (timer.Value < 0f)
{
commandBuffer.DestroyEntity(entity);
}
}).Run();
commandBuffer.Playback(EntityManager);
}
}
}