forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotatorInitSystem.cs
More file actions
55 lines (46 loc) · 1.62 KB
/
Copy pathRotatorInitSystem.cs
File metadata and controls
55 lines (46 loc) · 1.62 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
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
namespace HelloCube.GameObjectSync
{
[UpdateInGroup(typeof(InitializationSystemGroup))]
public partial struct RotatorInitSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<DirectoryManaged>();
state.RequireForUpdate<Execute.GameObjectSync>();
}
// This OnUpdate accesses managed objects, so it cannot be burst compiled.
public void OnUpdate(ref SystemState state)
{
var directory = SystemAPI.ManagedAPI.GetSingleton<DirectoryManaged>();
var ecb = new EntityCommandBuffer(Allocator.Temp);
// Instantiate the associated GameObject from the prefab.
foreach (var (goPrefab, entity) in
SystemAPI.Query<RotationSpeed>()
.WithNone<RotatorGO>()
.WithEntityAccess())
{
var go = GameObject.Instantiate(directory.RotatorPrefab);
// We can't add components to entities as we iterate over them, so we defer the change with an ECB.
ecb.AddComponent(entity, new RotatorGO(go));
}
ecb.Playback(state.EntityManager);
}
}
public class RotatorGO : IComponentData
{
public GameObject Value;
public RotatorGO(GameObject value)
{
Value = value;
}
// Every IComponentData class must have a no-arg constructor.
public RotatorGO()
{
}
}
}