forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefabSpawnerSystem.cs
More file actions
66 lines (60 loc) · 2.56 KB
/
Copy pathPrefabSpawnerSystem.cs
File metadata and controls
66 lines (60 loc) · 2.56 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
59
60
61
62
63
64
65
66
using System;
using Unity.Entities;
using Unity.Entities.Serialization;
using Unity.Mathematics;
using Unity.Scenes;
using Unity.Transforms;
using Random = Unity.Mathematics.Random;
public struct PrefabSpawner : IComponentData
{
public float SpawnsRemaining;
public float SpawnsPerSecond;
}
public struct PrefabSpawnerBufferElement : IBufferElementData
{
public EntityPrefabReference Prefab;
}
[RequireMatchingQueriesForUpdate]
public partial class PrefabSpawnerSystem : SystemBase
{
private BeginSimulationEntityCommandBufferSystem m_BeginSimECBSystem;
protected override void OnCreate()
{
m_BeginSimECBSystem = World.GetExistingSystemManaged<BeginSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = m_BeginSimECBSystem.CreateCommandBuffer().AsParallelWriter();
var rnd = new Random((uint)Environment.TickCount);
Entities.WithNone<RequestEntityPrefabLoaded>().ForEach((Entity entity, int entityInQueryIndex, ref PrefabSpawner spawner, in DynamicBuffer<PrefabSpawnerBufferElement> prefabs) =>
{
// Select a random prefab to load
ecb.AddComponent(entityInQueryIndex, entity, new RequestEntityPrefabLoaded {Prefab = prefabs[rnd.NextInt(prefabs.Length)].Prefab});
}).ScheduleParallel();
var dt = SystemAPI.Time.DeltaTime;
Entities.ForEach((Entity entity, int entityInQueryIndex, ref PrefabSpawner spawner, in PrefabLoadResult prefab) =>
{
var remaining = spawner.SpawnsRemaining;
if (remaining < 0.0f)
{
// No more instances left to spawn
ecb.DestroyEntity(entityInQueryIndex, entity);
return;
}
var newRemaining = remaining - dt * spawner.SpawnsPerSecond;
var spawnCount = (int) remaining - (int) newRemaining;
for (int i = 0; i < spawnCount; ++i)
{
var instance = ecb.Instantiate(entityInQueryIndex, prefab.PrefabRoot);
int index = i + (int) remaining;
#if !ENABLE_TRANSFORM_V1
ecb.SetComponent(entityInQueryIndex, instance, new LocalToWorldTransform {Value = UniformScaleTransform.FromPosition(new float3(index*((index&1)*2-1), 0, 0))});
#else
ecb.SetComponent(entityInQueryIndex, instance, new Translation {Value = new float3(index*((index&1)*2-1), 0, 0)});
#endif
}
spawner.SpawnsRemaining = newRemaining;
}).ScheduleParallel();
m_BeginSimECBSystem.AddJobHandleForProducer(Dependency);
}
}