forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityTracker.cs
More file actions
52 lines (46 loc) · 1.45 KB
/
Copy pathEntityTracker.cs
File metadata and controls
52 lines (46 loc) · 1.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
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;
using UnityEngine;
using UnityEngine.Jobs;
public class EntityTracker : MonoBehaviour {}
[UpdateInGroup(typeof(TransformSystemGroup))]
[UpdateAfter(typeof(EndFrameLocalToParentSystem))]
class SynchronizeGameObjectTransformsWithEntities : SystemBase
{
EntityQuery m_Query;
protected override void OnCreate()
{
base.OnCreate();
m_Query = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[]
{
typeof(EntityTracker),
typeof(Transform),
typeof(LocalToWorld)
}
});
}
protected override void OnUpdate()
{
var localToWorlds = m_Query.ToComponentDataArrayAsync<LocalToWorld>(Allocator.TempJob, out var jobHandle);
Dependency = new SyncTransforms
{
LocalToWorlds = localToWorlds
}.Schedule(m_Query.GetTransformAccessArray(), JobHandle.CombineDependencies(Dependency, jobHandle));
}
[BurstCompile]
struct SyncTransforms : IJobParallelForTransform
{
[DeallocateOnJobCompletion]
[ReadOnly] public NativeArray<LocalToWorld> LocalToWorlds;
public void Execute(int index, TransformAccess transform)
{
transform.position = LocalToWorlds[index].Position;
transform.rotation = LocalToWorlds[index].Rotation;
}
}
}