forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectionSystem.cs
More file actions
88 lines (81 loc) · 3.38 KB
/
Copy pathSectionSystem.cs
File metadata and controls
88 lines (81 loc) · 3.38 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
using Streaming.SceneManagement.Common;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Scenes;
using Unity.Transforms;
using UnityEngine;
namespace Streaming.SceneManagement.SectionMetadata
{
// Loads and unloads each sections as the relevant entities enter and leave the circles.
partial struct SectionSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Circle>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
NativeHashSet<Entity> toLoad = new NativeHashSet<Entity>(1, Allocator.Temp);
var sectionQuery = SystemAPI.QueryBuilder().WithAll<Circle, SceneSectionData>().Build();
var sectionEntities = sectionQuery.ToEntityArray(Allocator.Temp);
var circles = sectionQuery.ToComponentDataArray<Circle>(Allocator.Temp);
// Find all the sections that should be loaded based on the distances to the sphere
foreach (var transform in
SystemAPI.Query<RefRO<LocalTransform>>()
.WithAll<Relevant>())
{
for (int index = 0; index < circles.Length; ++index)
{
float3 distance = transform.ValueRO.Position - circles[index].Center;
distance.y = 0;
float radiusSq = circles[index].Radius;
Color debugColor = new Color(1f, 0f, 0f);
if (math.lengthsq(distance) < radiusSq * radiusSq)
{
toLoad.Add(sectionEntities[index]);
debugColor = new Color(0f, 0.5f, 0f);
}
DrawCircleXZ(circles[index].Center + new float3(0f, 0.2f, 0f),
circles[index].Radius, debugColor);
}
}
foreach (Entity sectionEntity in sectionEntities)
{
var sectionState = SceneSystem.GetSectionStreamingState(state.WorldUnmanaged, sectionEntity);
if (toLoad.Contains(sectionEntity))
{
if (sectionState == SceneSystem.SectionStreamingState.Unloaded)
{
// Load the section
state.EntityManager.AddComponent<RequestSceneLoaded>(sectionEntity);
}
}
else
{
if (sectionState != SceneSystem.SectionStreamingState.Unloaded)
{
// Unload the section
state.EntityManager.RemoveComponent<RequestSceneLoaded>(sectionEntity);
}
}
}
}
public static void DrawCircleXZ(float3 position, float radius, Color color, float divisions = 8f)
{
float angle = 0f;
float step = math.PI / divisions;
float PI2 = math.PI * 2f;
while (angle < PI2)
{
float3 begin = new float3(math.sin(angle), 0f, math.cos(angle)) * radius + position;
angle += step;
float3 end = new float3(math.sin(angle), 0f, math.cos(angle)) * radius + position;
Debug.DrawLine(begin, end, color);
}
}
}
}