forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnRandomCircleSystem.cs
More file actions
82 lines (70 loc) · 2.91 KB
/
Copy pathSpawnRandomCircleSystem.cs
File metadata and controls
82 lines (70 loc) · 2.91 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace Samples.Common
{
public class SpawnRandomCircleSystem : ComponentSystem
{
#pragma warning disable 649
struct Group
{
[ReadOnly]
public SharedComponentDataArray<SpawnRandomCircle> Spawner;
public ComponentDataArray<Position> Position;
public EntityArray Entity;
public readonly int Length;
}
[Inject] Group m_Group;
#pragma warning restore 649
protected override void OnUpdate()
{
while (m_Group.Length != 0)
{
var spawner = m_Group.Spawner[0];
var sourceEntity = m_Group.Entity[0];
var center = m_Group.Position[0].Value;
var entities = new NativeArray<Entity>(spawner.count, Allocator.Temp);
EntityManager.Instantiate(spawner.prefab, entities);
var positions = new NativeArray<float3>(spawner.count, Allocator.Temp);
if (spawner.spawnLocal)
{
GeneratePoints.RandomPointsOnCircle(new float3(), spawner.radius, ref positions);
for (int i = 0; i < spawner.count; i++)
{
var position = new Position
{
Value = positions[i]
};
EntityManager.SetComponentData(entities[i],position);
// Spawn Attach
var attach = EntityManager.CreateEntity();
EntityManager.AddComponentData(attach, new Attach
{
Parent = sourceEntity,
Child = entities[i]
});
}
}
else
{
GeneratePoints.RandomPointsOnCircle(center, spawner.radius, ref positions);
for (int i = 0; i < spawner.count; i++)
{
var position = new Position
{
Value = positions[i]
};
EntityManager.SetComponentData(entities[i],position);
}
}
entities.Dispose();
positions.Dispose();
EntityManager.RemoveComponent<SpawnRandomCircle>(sourceEntity);
// Instantiate & AddComponent & RemoveComponent calls invalidate the injected groups,
// so before we get to the next spawner we have to reinject them
UpdateInjectedComponentGroups();
}
}
}
}