forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnRandomObjectsAuthoring.cs
More file actions
105 lines (88 loc) · 3.45 KB
/
Copy pathSpawnRandomObjectsAuthoring.cs
File metadata and controls
105 lines (88 loc) · 3.45 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
class SpawnRandomObjectsAuthoring : SpawnRandomObjectsAuthoringBase<SpawnSettings>
{
}
abstract class SpawnRandomObjectsAuthoringBase<T> : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs
where T : struct, IComponentData, ISpawnSettings
{
#pragma warning disable 649
public GameObject prefab;
public float3 range = new float3(10f);
public int count;
#pragma warning restore 649
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var spawnSettings = new T
{
Prefab = conversionSystem.GetPrimaryEntity(prefab),
Position = transform.position,
Range = range,
Count = count
};
Configure(ref spawnSettings);
dstManager.AddComponentData(entity, spawnSettings);
}
internal virtual void Configure(ref T spawnSettings) { }
public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs) => referencedPrefabs.Add(prefab);
}
interface ISpawnSettings
{
Entity Prefab { get; set; }
float3 Position { get; set; }
float3 Range { get; set; }
int Count { get; set; }
}
struct SpawnSettings : IComponentData, ISpawnSettings
{
public Entity Prefab { get; set; }
public float3 Position { get; set; }
public float3 Range { get; set; }
public int Count { get; set; }
}
class SpawnRandomObjectsSystem : SpawnRandomObjectsSystemBase<SpawnSettings>
{
}
abstract class SpawnRandomObjectsSystemBase<T> : ComponentSystem where T : struct, IComponentData, ISpawnSettings
{
internal virtual void OnBeforeInstantiatePrefab(T spawnSettings) { }
internal virtual void ConfigureInstance(Entity instance, T spawnSettings) { }
protected override void OnUpdate()
{
Entities.ForEach((Entity entity, ref T spawnSettings) =>
{
var count = spawnSettings.Count;
OnBeforeInstantiatePrefab(spawnSettings);
var instances = new NativeArray<Entity>(count, Allocator.Temp);
EntityManager.Instantiate(spawnSettings.Prefab, instances);
var positions = new NativeArray<float3>(count, Allocator.Temp);
var rotations = new NativeArray<quaternion>(count, Allocator.Temp);
RandomPointsOnCircle(spawnSettings.Position, spawnSettings.Range, ref positions, ref rotations);
for (int i = 0; i < count; i++)
{
var instance = instances[i];
EntityManager.SetComponentData(instance, new Translation { Value = positions[i] });
EntityManager.SetComponentData(instance, new Rotation { Value = rotations[i] });
ConfigureInstance(instance, spawnSettings);
}
EntityManager.RemoveComponent<T>(entity);
});
}
static void RandomPointsOnCircle(float3 center, float3 range, ref NativeArray<float3> positions, ref NativeArray<quaternion> rotations)
{
var count = positions.Length;
// initialize the seed of the random number generator
var random = new Unity.Mathematics.Random();
random.InitState(10);
for (int i = 0; i < count; i++)
{
positions[i] = center + random.NextFloat3(-range, range);
rotations[i] = random.NextQuaternionRotation();
}
}
}