forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadingSystem.cs
More file actions
87 lines (78 loc) · 3.71 KB
/
Copy pathLoadingSystem.cs
File metadata and controls
87 lines (78 loc) · 3.71 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
using Unity.Collections;
using Unity.Entities;
using Unity.Scenes;
namespace Streaming.PrefabAndSceneReferences
{
[RequireMatchingQueriesForUpdate]
[WorldSystemFilter(WorldSystemFilterFlags.Default | WorldSystemFilterFlags.Editor)]
public partial struct LoadingSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
var query = SystemAPI.QueryBuilder().WithAll<SceneReference>().Build();
var sceneRefs = query.ToComponentDataArray<SceneReference>(Allocator.Temp);
var entities = query.ToEntityArray(Allocator.Temp);
var ecb = new EntityCommandBuffer(Allocator.Temp);
// Load entity scene and add a cleanup component referencing the entity scene to
// unload when the primary entity will be destroyed
for (int i = 0; i < entities.Length; i++)
{
var entity = entities[i];
var sceneEntity = SceneSystem.LoadSceneAsync(state.World.Unmanaged, sceneRefs[i].Value);
ecb.AddComponent(entity, new CleanupSceneReference()
{
SceneToUnload = sceneEntity
});
ecb.RemoveComponent<SceneReference>(entity);
}
// Load the PrefabReferences
foreach (var (prefabRef, entity) in
SystemAPI.Query<RefRO<PrefabReference>>()
.WithNone<RequestEntityPrefabLoaded>()
.WithEntityAccess())
{
ecb.AddComponent(entity, new RequestEntityPrefabLoaded
{
Prefab = prefabRef.ValueRO.Value
});
}
// Instantiate the PrefabReferences
foreach (var (loadedPrefab, entity) in
SystemAPI.Query<RefRO<PrefabLoadResult>>()
.WithAll<PrefabReference>()
.WithEntityAccess())
{
var prefabEntity = ecb.Instantiate(loadedPrefab.ValueRO.PrefabRoot);
ecb.AddComponent(entity, new CleanupPrefabReference()
{
PrefabToUnload = prefabEntity
});
ecb.RemoveComponent<PrefabReference>(entity);
ecb.RemoveComponent<RequestEntityPrefabLoaded>(entity);
}
ecb.Playback(state.EntityManager);
// Unload the previously manually loaded entity scene after the subscene is being destroyed
query = SystemAPI.QueryBuilder().WithAll<CleanupSceneReference>().WithNone<SceneTag>().Build();
var cleanupSceneRefs = query.ToComponentDataArray<CleanupSceneReference>(Allocator.Temp);
entities = query.ToEntityArray(Allocator.Temp);
ecb = new EntityCommandBuffer(Allocator.Temp);
for (int i = 0; i < entities.Length; i++)
{
var cleanupSceneRef = cleanupSceneRefs[i];
SceneSystem.UnloadScene(state.World.Unmanaged, cleanupSceneRef.SceneToUnload);
ecb.DestroyEntity(cleanupSceneRef.SceneToUnload);
ecb.RemoveComponent<CleanupSceneReference>(entities[i]);
}
// Unload the previously manually instantiated entity prefabs after the subscene is being destroyed
foreach (var (prefabRef, entity) in
SystemAPI.Query<RefRO<CleanupPrefabReference>>()
.WithNone<SceneTag>()
.WithEntityAccess())
{
ecb.DestroyEntity(prefabRef.ValueRO.PrefabToUnload);
ecb.RemoveComponent<CleanupPrefabReference>(entity);
}
ecb.Playback(state.EntityManager);
}
}
}