forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneStateSystem.cs
More file actions
75 lines (68 loc) · 2.88 KB
/
Copy pathSceneStateSystem.cs
File metadata and controls
75 lines (68 loc) · 2.88 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
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Scenes;
namespace Streaming.SceneManagement.SceneState
{
[RequireMatchingQueriesForUpdate]
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
public partial struct SceneStateSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<SceneReference>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var sceneQuery = SystemAPI.QueryBuilder().WithAll<SceneReference>().Build();
var scenes = sceneQuery.ToComponentDataArray<SceneReference>(Allocator.Temp);
// We cannot use a foreach query here because the SceneSystem methods add and remove components,
// which is not allowed inside a foreach query.
for (int index = 0; index < scenes.Length; ++index)
{
var scene = scenes[index];
scene.StreamingState = SceneSystem.GetSceneStreamingState(state.WorldUnmanaged, scene.EntityScene);
// The LoadingAction is set when the user clicks a button in the UI.
switch (scene.LoadingAction)
{
case LoadingAction.LoadAll:
case LoadingAction.LoadMeta:
{
var loadParam = new SceneSystem.LoadParameters
{
AutoLoad = (scene.LoadingAction == LoadingAction.LoadAll)
};
if (scene.EntityScene == default)
{
scene.EntityScene =
SceneSystem.LoadSceneAsync(state.WorldUnmanaged, scene.SceneAsset, loadParam);
}
else
{
SceneSystem.LoadSceneAsync(state.WorldUnmanaged, scene.EntityScene, loadParam);
}
break;
}
case LoadingAction.UnloadAll:
{
SceneSystem.UnloadScene(state.WorldUnmanaged, scene.EntityScene,
SceneSystem.UnloadParameters.DestroyMetaEntities);
scene.EntityScene = default;
break;
}
case LoadingAction.UnloadEntities:
{
SceneSystem.UnloadScene(state.WorldUnmanaged, scene.EntityScene);
break;
}
}
scene.LoadingAction = LoadingAction.None;
scenes[index] = scene;
}
// Copy the values in the array back to the actual components.
sceneQuery.CopyFromComponentDataArray(scenes);
}
}
}