forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetLoadingSystem.cs
More file actions
209 lines (192 loc) · 9.25 KB
/
Copy pathAssetLoadingSystem.cs
File metadata and controls
209 lines (192 loc) · 9.25 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Unity.Collections;
using Unity.Entities;
using Unity.Entities.Content;
using Unity.Loading;
using Unity.Mathematics;
using Unity.Scenes;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Streaming.AssetManagement
{
#if !UNITY_DISABLE_MANAGED_COMPONENTS
public partial struct AssetLoadingSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
var noLoadingQuery = SystemAPI.QueryBuilder().WithAll<References>()
.WithNone<Loading, RequestUnload>().Build();
// This uncommented line would add the Loading components, but they would all be null:
// state.EntityManager.AddComponent<Loading>(noLoadingQuery);
// Instead we must add a new Loading to each entity individually:
foreach (var entity in noLoadingQuery.ToEntityArray(Allocator.Temp))
{
state.EntityManager.AddComponentData(entity, new Loading());
}
var query = SystemAPI.QueryBuilder().WithAll<References, Loading>()
.WithNone<RequestUnload>().Build();
var referencesArray = query.ToComponentDataArray<References>(Allocator.Temp);
var loadingArray = query.ToComponentArray<Loading>();
// We cannot use SystemAPI.Query for this loop because we need to
// call methods that make structural changes in the loop.
for (int index = 0; index < referencesArray.Length; ++index)
{
var refs = referencesArray[index];
var loading = loadingArray[index];
// Load Entity Scene
if (loading.EntityScene == Entity.Null && refs.EntitySceneReference.IsReferenceValid)
{
loading.EntityScene =
SceneSystem.LoadSceneAsync(state.WorldUnmanaged, refs.EntitySceneReference);
}
// Load Entity Prefab
if (refs.EntityPrefabReference.IsReferenceValid)
{
if (loading.EntityPrefab == Entity.Null)
{
loading.EntityPrefab =
SceneSystem.LoadPrefabAsync(state.WorldUnmanaged, refs.EntityPrefabReference);
}
else if (loading.EntityPrefabInstance == Entity.Null &&
SceneSystem.GetSceneStreamingState(state.WorldUnmanaged, loading.EntityPrefab) ==
SceneSystem.SceneStreamingState.LoadedSuccessfully)
{
var prefabRoot = state.EntityManager.GetComponentData<PrefabRoot>(loading.EntityPrefab);
loading.EntityPrefabInstance = state.EntityManager.Instantiate(prefabRoot.Root);
}
}
// Load GameObject Scene
if (!loading.GameObjectScene.IsValid() && refs.GameObjectSceneReference.IsReferenceValid)
{
loading.GameObjectScene = refs.GameObjectSceneReference.LoadAsync(new ContentSceneParameters
{
autoIntegrate = true, loadSceneMode = LoadSceneMode.Additive,
localPhysicsMode = LocalPhysicsMode.None
});
}
// Load GameObject Prefab
if (loading.GameObjectPrefabInstance == null &&
refs.GameObjectPrefabReference.IsReferenceValid)
{
if (refs.GameObjectPrefabReference.LoadingStatus == ObjectLoadingStatus.None)
{
refs.GameObjectPrefabReference.LoadAsync();
}
if (refs.GameObjectPrefabReference.LoadingStatus == ObjectLoadingStatus.Completed)
{
loading.GameObjectPrefabInstance =
Object.Instantiate(refs.GameObjectPrefabReference.Result);
}
}
// Load Shader
if (loading.ShaderInstance == null && refs.ShaderReference.IsReferenceValid)
{
if (refs.ShaderReference.LoadingStatus == ObjectLoadingStatus.None)
{
refs.ShaderReference.LoadAsync();
}
else if (refs.ShaderReference.LoadingStatus == ObjectLoadingStatus.Completed)
{
// Create an object to display the loaded Texture
loading.ShaderInstance = refs.ShaderReference.Result;
}
}
// Load Mesh
float instancesXOffset = 2.5f;
if (loading.MeshGameObjectInstance == null && refs.MeshReference.IsReferenceValid)
{
if (refs.MeshReference.LoadingStatus == ObjectLoadingStatus.None)
{
refs.MeshReference.LoadAsync();
}
else if (refs.MeshReference.LoadingStatus == ObjectLoadingStatus.Completed &&
loading.ShaderInstance)
{
// Create an object to display the loaded Mesh
loading.MeshGameObjectInstance = CreateObjectWithMesh(
refs.MeshReference.Result,
loading.ShaderInstance,
"MeshGameObjectInstance",
new float3(instancesXOffset, 0f, 0f),
quaternion.RotateY(3.1416f / 2)
);
}
}
// Load Material
if (loading.MaterialGameObjectInstance == null &&
refs.MaterialReference.IsReferenceValid)
{
if (refs.MaterialReference.LoadingStatus == ObjectLoadingStatus.None)
refs.MaterialReference.LoadAsync();
else if (refs.MaterialReference.LoadingStatus == ObjectLoadingStatus.Completed)
{
// Create an object to display the loaded Material
loading.MaterialGameObjectInstance = CreateObjectWithMaterial(
refs.MaterialReference.Result,
"MaterialGameObjectInstance",
new float3(instancesXOffset, 2f, 0f)
);
}
}
// Load Texture
if (loading.TextureGameObjectInstance == null && refs.TextureReference.IsReferenceValid)
{
if (refs.TextureReference.LoadingStatus == ObjectLoadingStatus.None)
{
refs.TextureReference.LoadAsync();
}
else if (refs.TextureReference.LoadingStatus == ObjectLoadingStatus.Completed &&
loading.ShaderInstance)
{
// Create an object to display the loaded Texture
loading.TextureGameObjectInstance = CreateObjectWithTexture(
refs.TextureReference.Result,
loading.ShaderInstance,
"TextureGameObjectInstance",
new float3(instancesXOffset, 4f, 0f)
);
}
}
}
}
public GameObject CreateObjectWithMesh(Mesh mesh, Shader shader, string name, float3 position,
quaternion rotation)
{
// Create an object to display the mesh
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.name = name;
var transform = obj.transform;
transform.position = position;
transform.rotation = rotation;
var meshFilter = obj.GetComponent<MeshFilter>();
meshFilter.sharedMesh = mesh;
var renderer = obj.GetComponent<MeshRenderer>();
renderer.sharedMaterial = new Material(shader);
return obj;
}
public GameObject CreateObjectWithMaterial(Material material, string name, float3 position)
{
// Create an object to display the material
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.name = name;
var transform = obj.transform;
transform.position = position;
var meshRenderer = obj.GetComponent<MeshRenderer>();
meshRenderer.sharedMaterial = material;
return obj;
}
public GameObject CreateObjectWithTexture(Texture texture, Shader shader, string name, float3 position)
{
// Create an object to display the texture
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.name = name;
var transform = obj.transform;
transform.position = position;
var meshRenderer = obj.GetComponent<MeshRenderer>();
var material = new Material(shader);
material.mainTexture = texture;
meshRenderer.sharedMaterial = material;
return obj;
}
}
#endif
}