-
Notifications
You must be signed in to change notification settings - Fork 1
/
SceneLoader.cs
107 lines (92 loc) · 3.84 KB
/
SceneLoader.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using Unity.Collections;
using Unity.Entities;
using Unity.Entities.Serialization;
using Unity.Scenes;
using UnityEngine;
namespace PolkaDOTS.Deployment
{
// Runtime component, SceneSystem uses EntitySceneReference to identify scenes.
public struct AuthoringSceneReference : IComponentData
{
public EntitySceneReference SceneReference;
}
public struct LoadAuthoringSceneRequest : IComponentData
{
public WorldUnmanaged world;
}
#if UNITY_EDITOR
// Authoring component, a SceneAsset can only be used in the Editor
public class SceneLoader : MonoBehaviour
{
public UnityEditor.SceneAsset Scene;
class Baker : Baker<SceneLoader>
{
public override void Bake(SceneLoader authoring)
{
Debug.Log($"Run authoring scene baking!");
var reference = new EntitySceneReference(authoring.Scene);
var entity = GetEntity(TransformUsageFlags.None);
AddComponent(entity, new AuthoringSceneReference()
{
SceneReference = reference
});
}
}
}
#endif
/// <summary>
/// Run in Deployment world to call scene loads on created worlds. Fix for inconsistent authoring scene loading.
/// There is likely a better method.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.Disabled)] // Don't automatically add to any worlds
[UpdateInGroup(typeof(InitializationSystemGroup))]
[RequireMatchingQueriesForUpdate]
public partial class AuthoringSceneLoaderSystem : SystemBase
{
private EntityQuery newRequests;
private EntityQuery authoringSceneQuery;
protected override void OnCreate()
{
newRequests = GetEntityQuery(typeof(LoadAuthoringSceneRequest));
authoringSceneQuery = GetEntityQuery(typeof(AuthoringSceneReference));
}
protected override void OnUpdate()
{
var requests = newRequests.ToComponentDataArray<LoadAuthoringSceneRequest>(Allocator.Temp);
var authoringScenes = authoringSceneQuery.ToComponentDataArray<AuthoringSceneReference>(Allocator.Temp);
// Can't use a foreach with a query as SceneSystem.LoadSceneAsync does structural changes
for (var i = 0; i < requests.Length; i += 1)
{
var world = requests[i].world;
Debug.Log($"Loading authoring scene in world {world.Name}");
SceneSystem.LoadSceneAsync(world, authoringScenes[0].SceneReference);
}
requests.Dispose();
authoringScenes.Dispose();
EntityManager.DestroyEntity(newRequests);
}
}
/// <summary>
/// Run in all worlds to spawn the authoring scene within this world, without needing the deployment service.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation | WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
[UpdateInGroup(typeof(InitializationSystemGroup))]
[RequireMatchingQueriesForUpdate]
public partial class AutoAuthoringSceneLoaderSystem : SystemBase
{
private EntityQuery authoringSceneQuery;
protected override void OnCreate()
{
authoringSceneQuery = GetEntityQuery(typeof(AuthoringSceneReference));
}
protected override void OnUpdate()
{
var authoringScenes = authoringSceneQuery.ToComponentDataArray<AuthoringSceneReference>(Allocator.Temp);
Debug.Log($"AutoLoading authoring scene in world {World.Unmanaged.Name}");
SceneSystem.LoadSceneAsync(World.Unmanaged, authoringScenes[0].SceneReference);
authoringScenes.Dispose();
EntityManager.DestroyEntity(authoringSceneQuery);
Enabled = false;
}
}
}