forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOffsetSystem.cs
More file actions
55 lines (50 loc) · 2.01 KB
/
Copy pathOffsetSystem.cs
File metadata and controls
55 lines (50 loc) · 2.01 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
using Streaming.SceneManagement.Common;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace Streaming.SceneManagement.SubsceneInstancing
{
// After a subscene instance is loaded, this system moves its entities by the instance's offset.
// This system runs in a separate world before the loaded entities are moved to the main world.
[WorldSystemFilter(WorldSystemFilterFlags.ProcessAfterLoad)]
public partial struct OffsetSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Offset>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var offsetQuery = SystemAPI.QueryBuilder().WithAll<Offset>().Build();
var offsets = offsetQuery.ToComponentDataArray<Offset>(Allocator.Temp);
state.EntityManager.DestroyEntity(offsetQuery);
foreach (var offset in offsets)
{
// Apply the offset to all the dynamic entities
foreach (var transform in
SystemAPI.Query<RefRW<LocalTransform>>())
{
transform.ValueRW.Position += offset.Value;
}
// Apply the offset to all non-dynamic entities
var offsetMatrix = float4x4.Translate(offset.Value);
foreach (var transform in
SystemAPI.Query<RefRW<LocalToWorld>>()
.WithNone<LocalTransform>())
{
transform.ValueRW.Value = math.mul(offsetMatrix, transform.ValueRW.Value);
}
// Apply the offset to the center of oscillation
foreach (var oscillating in
SystemAPI.Query<RefRW<Oscillating>>())
{
oscillating.ValueRW.Center += offset.Value;
}
}
}
}
}