This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathAssetService.cs
More file actions
151 lines (127 loc) · 5.59 KB
/
AssetService.cs
File metadata and controls
151 lines (127 loc) · 5.59 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace Microsoft.MixedReality.SpectatorView
{
internal class AssetService : Singleton<AssetService>, IAssetCache
{
private TextureAssetCache textureAssets;
private MeshAssetCache meshAssets;
private MaterialPropertyAssetCache materialPropertyAssets;
private readonly Dictionary<ShortID, IAssetSerializer<Texture>> textureSerializers = new Dictionary<ShortID, IAssetSerializer<Texture>>();
protected virtual void Start()
{
textureAssets = LookupAssetCache<TextureAssetCache>();
meshAssets = LookupAssetCache<MeshAssetCache>();
materialPropertyAssets = LookupAssetCache<MaterialPropertyAssetCache>();
if (textureAssets != null)
{
textureSerializers.Add(textureAssets.GetID(), textureAssets);
}
}
private T LookupAssetCache<T>() where T : AssetCache
{
T toReturn = AssetCache.LoadAssetCache<T>();
if (toReturn == null)
{
Debug.LogWarning($"Could not find an asset cache of type: {typeof(T).Name}");
}
return toReturn;
}
public IEnumerable<MaterialPropertyAsset> GetMaterialProperties(string shaderName)
{
return materialPropertyAssets?.GetMaterialProperties(shaderName) ?? Array.Empty<MaterialPropertyAsset>();
}
public void RegisterTextureSerializer(IAssetSerializer<Texture> textureSerializer)
{
textureSerializers.Add(textureSerializer.GetID(), textureSerializer);
}
public bool TrySerializeTexture(BinaryWriter writer, Texture texture)
{
foreach (KeyValuePair<ShortID, IAssetSerializer<Texture>> serializerPair in textureSerializers)
{
if (serializerPair.Value.CanSerialize(texture))
{
writer.Write(serializerPair.Key.Value);
serializerPair.Value.Serialize(writer, texture);
return true;
}
}
writer.Write((ushort)0);
return false;
}
public bool TryDeserializeTexture(BinaryReader reader, out Texture texture)
{
ShortID shortID = new ShortID(reader.ReadUInt16());
IAssetSerializer<Texture> textureSerializer;
if (textureSerializers.TryGetValue(shortID, out textureSerializer))
{
texture = textureSerializer.Deserialize(reader);
return true;
}
else
{
texture = null;
return false;
}
}
public AssetId GetMeshId(Mesh mesh)
{
return meshAssets?.GetAssetId(mesh) ?? AssetId.Empty;
}
public bool AttachMeshFilter(GameObject gameObject, AssetId assetId)
{
ComponentExtensions.EnsureComponent<MeshRenderer>(gameObject);
Mesh mesh = meshAssets.GetAsset(assetId);
if (mesh != null)
{
MeshFilter filter = ComponentExtensions.EnsureComponent<MeshFilter>(gameObject);
filter.sharedMesh = mesh;
return true;
}
return false;
}
public bool AttachDynamicMeshFilter(GameObject gameObject, AssetId assetId, Mesh mesh)
{
ComponentExtensions.EnsureComponent<MeshRenderer>(gameObject);
MeshFilter filter = ComponentExtensions.EnsureComponent<MeshFilter>(gameObject);
filter.sharedMesh = mesh;
return true;
}
public bool AttachSkinnedMeshRenderer(GameObject gameObject, AssetId assetId)
{
Mesh mesh = meshAssets.GetAsset(assetId);
if (mesh != null)
{
SkinnedMeshRenderer renderer = ComponentExtensions.EnsureComponent<SkinnedMeshRenderer>(gameObject);
renderer.sharedMesh = mesh;
return true;
}
return false;
}
public void UpdateAssetCache()
{
AssetCache.GetOrCreateAssetCache<TextureAssetCache>().UpdateAssetCache();
AssetCache.GetOrCreateAssetCache<MeshAssetCache>().UpdateAssetCache();
AssetCache.GetOrCreateAssetCache<MaterialPropertyAssetCache>().UpdateAssetCache();
AssetCache.GetOrCreateAssetCache<CustomShaderPropertyAssetCache>().UpdateAssetCache();
}
public void ClearAssetCache()
{
AssetCache.GetOrCreateAssetCache<TextureAssetCache>().ClearAssetCache();
AssetCache.GetOrCreateAssetCache<MeshAssetCache>().ClearAssetCache();
AssetCache.GetOrCreateAssetCache<MaterialPropertyAssetCache>().ClearAssetCache();
AssetCache.GetOrCreateAssetCache<CustomShaderPropertyAssetCache>().ClearAssetCache();
}
public void SaveAssets()
{
AssetCache.GetOrCreateAssetCache<TextureAssetCache>().SaveAssets();
AssetCache.GetOrCreateAssetCache<MeshAssetCache>().SaveAssets();
AssetCache.GetOrCreateAssetCache<MaterialPropertyAssetCache>().SaveAssets();
AssetCache.GetOrCreateAssetCache<CustomShaderPropertyAssetCache>().SaveAssets();
}
}
}