forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetUnloadingSystem.cs
More file actions
114 lines (98 loc) · 4.24 KB
/
Copy pathAssetUnloadingSystem.cs
File metadata and controls
114 lines (98 loc) · 4.24 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
using Unity.Collections;
using Unity.Entities;
using Unity.Entities.Content;
using Unity.Scenes;
using UnityEngine;
namespace Streaming.AssetManagement
{
#if !UNITY_DISABLE_MANAGED_COMPONENTS
public partial struct AssetUnloadingSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
var query = SystemAPI.QueryBuilder().WithAll<References, Loading, 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];
// Unload Entity Scene
if (loading.EntityScene != Entity.Null)
{
SceneSystem.UnloadScene(state.WorldUnmanaged, loading.EntityScene,
SceneSystem.UnloadParameters.DestroyMetaEntities);
}
// Unload Entity Prefab
if (loading.EntityPrefabInstance != Entity.Null)
{
state.EntityManager.DestroyEntity(loading.EntityPrefabInstance);
}
if (loading.EntityPrefab != Entity.Null)
{
SceneSystem.UnloadScene(state.WorldUnmanaged, loading.EntityPrefab,
SceneSystem.UnloadParameters.DestroyMetaEntities);
}
// Unload GameObject Scene
if (loading.GameObjectScene.IsValid())
{
refs.GameObjectSceneReference.Unload(ref loading.GameObjectScene);
}
// Unload GameObject Prefab
if (loading.GameObjectPrefabInstance != null)
{
Object.Destroy(loading.GameObjectPrefabInstance);
}
if (refs.GameObjectPrefabReference.LoadingStatus != ObjectLoadingStatus.None)
{
refs.GameObjectPrefabReference.Release();
}
// Unload Mesh
if (loading.MeshGameObjectInstance)
{
var renderer = loading.MeshGameObjectInstance.GetComponent<MeshRenderer>();
var material = renderer.sharedMaterial;
Object.Destroy(material);
GameObject.Destroy(loading.MeshGameObjectInstance);
}
if (refs.MeshReference.LoadingStatus != ObjectLoadingStatus.None)
{
refs.MeshReference.Release();
}
// Unload Material
if (loading.MaterialGameObjectInstance)
{
GameObject.Destroy(loading.MaterialGameObjectInstance);
}
if (refs.MaterialReference.LoadingStatus != ObjectLoadingStatus.None)
{
refs.MaterialReference.Release();
}
// Unload Texture
if (loading.TextureGameObjectInstance)
{
var renderer = loading.TextureGameObjectInstance.GetComponent<MeshRenderer>();
var material = renderer.sharedMaterial;
Object.Destroy(material);
GameObject.Destroy(loading.TextureGameObjectInstance);
}
if (refs.TextureReference.LoadingStatus != ObjectLoadingStatus.None)
{
refs.TextureReference.Release();
}
// Unload Shader
if (refs.ShaderReference.LoadingStatus != ObjectLoadingStatus.None)
{
refs.ShaderReference.Release();
}
}
// Remove Loading
var noLoadingStateQuery = SystemAPI.QueryBuilder()
.WithAll<References, Loading, RequestUnload>().Build();
state.EntityManager.RemoveComponent<Loading>(noLoadingStateQuery);
}
}
#endif
}