-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExternalAssetLoader.cs
350 lines (298 loc) · 15.5 KB
/
ExternalAssetLoader.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Events;
using Newtonsoft.Json;
namespace FortySevenE.ExternalAssetLoading
{
public class ExternalAssetLoader : MonoBehaviour
{
private readonly string[] _videoExtension = { ".mp4", ".mov", ".mkv", ".webm" };
private readonly string[] _audioExtension = { ".wav", ".mp3", ".ogg", ".aac" };
private readonly string[] _imageExtension = { ".jpg", ".png", ".tiff", ".jpeg", ".psd", ".tga" };
public static ExternalAssetLoader Instance;
public static event Action LocalAssetLoadingFinished;
public static readonly string AssetKeyInstancePrefix = "[{0}]";
public ExternalLoadedAssetCollection LocalLoadedAssetCollection { get; private set; }
[Header("Local Assets: Will add all the files in _localAssetsFolder to the asset list with the key of their file name without ext")]
[JsonProperty("LocalAssetsFolder")] [SerializeField] private string _localAssetsFolderPath = "[StreamingAssetsPath]/AssetLoaderLocalAssets";
[JsonProperty("DownloadAssetsFolder")] [SerializeField] private string _downloadAssetFolderPath = "[PersistentDataPath]/AssetLoaderDownloads";
[Header("GUI Settings")]
[SerializeField] private AssetLoadingGUISettings _assetLoadingGUISettings;
[Header("Assets Status (RuntimeDebug)")]
[SerializeField] private List<ExternalAssetInfo> _localAssetList;
[Header("Callbacks")]
[SerializeField] private UnityEvent LocalAssetLoadingFinished_UnityEvent;
public bool IsBusyLoading { get; private set; }
private ExternalLoadedAssetCollection _currentLoadingAssetCollection;
private Queue<GetAssetsRequest> _pendingLoadingRequests = new Queue<GetAssetsRequest>();
private Coroutine _assetLoadingCoroutine;
private UnityWebRequest _currentAssetLoadingWebRequest;
private bool _showLoadingGUI;
private GUIStyle _loadingStatusGUIStyle;
private string _assetLoadingStatusString;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else if (Instance != this)
{
Destroy(Instance);
}
}
private void Start()
{
//Setup GUI
_loadingStatusGUIStyle = new GUIStyle();
_loadingStatusGUIStyle.alignment = TextAnchor.LowerLeft;
_loadingStatusGUIStyle.normal.textColor = _assetLoadingGUISettings.textColor;
_loadingStatusGUIStyle.fontSize = 12 * Screen.width / 1080;
//Load local assets
LoadAssets(new GetAssetsRequest(
_localAssetsFolderPath,
showLoadingGUI: true,
onCompleted: (ExternalLoadedAssetCollection loadedAssetCollection) =>
{
LocalLoadedAssetCollection = loadedAssetCollection;
_localAssetList = new List<ExternalAssetInfo>(loadedAssetCollection.allAvailableAssets.Values);
LocalAssetLoadingFinished_UnityEvent?.Invoke();
LocalAssetLoadingFinished?.Invoke();
}
));
}
private void Update()
{
if (_pendingLoadingRequests.Count > 0 && !IsBusyLoading)
{
ProcessLoadAssetsRequest(_pendingLoadingRequests.Dequeue());
}
}
public void LoadAssets(GetAssetsRequest request)
{
_pendingLoadingRequests.Enqueue(request);
}
private void ProcessLoadAssetsRequest(GetAssetsRequest request)
{
IsBusyLoading = true;
var foundAssetInfos = new List<ExternalAssetInfo>();
#region Looking for all available assets
string absLocalAssetsFolder = UnityPathHelper.DecodeUnityPlatformSpecificFolder(request.folderPath);
if (Directory.Exists(absLocalAssetsFolder))
{
string[] assetFiles = Directory.GetFiles(absLocalAssetsFolder);
if (assetFiles != null && assetFiles.Length > 0)
{
string fileExtension = "";
string fileNameWithoutExtension = "";
foreach (string assetFileFullPath in assetFiles)
{
fileExtension = Path.GetExtension(assetFileFullPath).ToLower();
//Ignore meta files
if (fileExtension == ".meta")
{
continue;
}
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(assetFileFullPath);
ExternalAssetInfo localAssetInfo = new ExternalAssetInfo();
localAssetInfo.key = fileNameWithoutExtension;
localAssetInfo.absPath = assetFileFullPath;
localAssetInfo.type = GetAssetType(assetFileFullPath);
foundAssetInfos.Add(localAssetInfo);
}
}
}
#endregion
#region Load assets
_showLoadingGUI = request.showLoadingGUI;
if (_assetLoadingCoroutine != null)
{
StopCoroutine(_assetLoadingCoroutine);
_assetLoadingCoroutine = null;
}
if (_currentAssetLoadingWebRequest != null)
{
_currentAssetLoadingWebRequest.Abort();
_currentAssetLoadingWebRequest = null;
}
_assetLoadingCoroutine = StartCoroutine(
LoadAssetsCoroutine(
foundAssetInfos,
(ExternalLoadedAssetCollection loadedAssetCollection) =>
{
try
{
request.onCompleted?.Invoke(loadedAssetCollection);
}
catch (Exception e)
{
Debug.LogWarning($"[{GetType().Name}]Exception happened on AssetLoading callbacks: {e}");
}
CleanUpAfterAssetLoadingFinished();
})
);
#endregion
}
private IEnumerator LoadAssetsCoroutine(IEnumerable<ExternalAssetInfo> assetInfos, Action<ExternalLoadedAssetCollection> onCompleteCallback)
{
_currentLoadingAssetCollection = new ExternalLoadedAssetCollection();
foreach (ExternalAssetInfo assetInfo in assetInfos)
{
if (assetInfo.type == ExternalAssetType.Unknown)
{
assetInfo.type = GetAssetType(assetInfo.absPath);
}
if (assetInfo.type == ExternalAssetType.Unknown)
{
Debug.LogWarningFormat("[{0}] Cannot identify the assset type of {1}. Skip", GetType().Name, assetInfo.key);
continue;
}
//Load assets via UnityWebRequest
switch(assetInfo.type)
{
case ExternalAssetType.Image:
_currentAssetLoadingWebRequest = UnityWebRequestTexture.GetTexture(assetInfo.absPath);
break;
case ExternalAssetType.Audio:
_currentAssetLoadingWebRequest = UnityWebRequestMultimedia.GetAudioClip(assetInfo.absPath, AudioType.UNKNOWN);
break;
case ExternalAssetType.Video:
_currentAssetLoadingWebRequest = UnityWebRequest.Get(assetInfo.absPath);
break;
}
yield return _currentAssetLoadingWebRequest.SendWebRequest();
if (!string.IsNullOrEmpty(_currentAssetLoadingWebRequest.error))
{
Debug.LogWarningFormat("[{0}] {1}: Failed to load <color=blue>{2}</color>", GetType().Name, _currentAssetLoadingWebRequest.error, _currentAssetLoadingWebRequest.url);
continue;
}
//If it is a network path, save the download locally
if (!_currentAssetLoadingWebRequest.uri.IsFile)
{
string downloadFolderPath = UnityPathHelper.DecodeUnityPlatformSpecificFolder(_downloadAssetFolderPath);
if (!Directory.Exists(downloadFolderPath))
{
Directory.CreateDirectory(downloadFolderPath);
}
string downloadedFilePath = Path.Combine(downloadFolderPath, Path.GetFileName(assetInfo.absPath));
File.WriteAllBytes(downloadedFilePath, _currentAssetLoadingWebRequest.downloadHandler.data);
assetInfo.absPath = downloadedFilePath;
}
switch (assetInfo.type)
{
case ExternalAssetType.Image:
//Generate mipmap for loaded textures
Texture2D loadedTexture = DownloadHandlerTexture.GetContent(_currentAssetLoadingWebRequest);
Texture2D mipmappedTexture = new Texture2D(loadedTexture.width, loadedTexture.height, loadedTexture.format, true, true);
mipmappedTexture.SetPixels(loadedTexture.GetPixels());
mipmappedTexture.Apply();
if (_currentLoadingAssetCollection.preLoadedTextures.ContainsKey(assetInfo.key))
{
_currentLoadingAssetCollection.preLoadedTextures[assetInfo.key] = mipmappedTexture;
}
else
{
_currentLoadingAssetCollection.preLoadedTextures.Add(assetInfo.key, mipmappedTexture);
}
break;
case ExternalAssetType.Audio:
if (_currentLoadingAssetCollection.preLoadedAudioClips.ContainsKey(assetInfo.key))
{
_currentLoadingAssetCollection.preLoadedAudioClips[assetInfo.key] = DownloadHandlerAudioClip.GetContent(_currentAssetLoadingWebRequest);
}
else
{
_currentLoadingAssetCollection.preLoadedAudioClips.Add(assetInfo.key, DownloadHandlerAudioClip.GetContent(_currentAssetLoadingWebRequest));
}
break;
case ExternalAssetType.Video:
if (_currentLoadingAssetCollection.keyToAbsVideoPaths.ContainsKey(assetInfo.key))
{
_currentLoadingAssetCollection.keyToAbsVideoPaths[assetInfo.key] = assetInfo.absPath;
}
else
{
_currentLoadingAssetCollection.keyToAbsVideoPaths.Add(assetInfo.key, assetInfo.absPath);
}
break;
}
_currentLoadingAssetCollection.allAvailableAssets.Add(assetInfo.key, assetInfo);
}
onCompleteCallback?.Invoke(_currentLoadingAssetCollection);
_currentLoadingAssetCollection = null;
}
private ExternalAssetType GetAssetType (string assetPath)
{
string assetExtension = Path.GetExtension(assetPath).ToLower();
if (_videoExtension.Contains(assetExtension))
{
return ExternalAssetType.Video;
}
else if (_audioExtension.Contains(assetExtension))
{
return ExternalAssetType.Audio;
}
else if (_imageExtension.Contains(assetExtension))
{
return ExternalAssetType.Image;
}
return ExternalAssetType.Unknown;
}
private void CleanUpAfterAssetLoadingFinished()
{
IsBusyLoading = false;
_showLoadingGUI = false;
_assetLoadingCoroutine = null;
_currentAssetLoadingWebRequest = null;
}
private void OnGUI()
{
if (_showLoadingGUI)
{
#region Loading GUI Background
if (_assetLoadingGUISettings.background != null)
{
GUI.DrawTexture(new Rect(Vector2.zero, new Vector2(Screen.width, Screen.height)), _assetLoadingGUISettings.background, ScaleMode.StretchToFill, alphaBlend: true, imageAspect: 0, color: _assetLoadingGUISettings.backgroundColor, borderWidth: 0, borderRadius: 0);
}
else
{
GUI.DrawTexture(new Rect(Vector2.zero, new Vector2(Screen.width, Screen.height)), Texture2D.whiteTexture, ScaleMode.StretchToFill, alphaBlend: true, imageAspect: 0, color: _assetLoadingGUISettings.backgroundColor, borderWidth: 0, borderRadius: 0);
}
#endregion
#region Loading Animation
if (_assetLoadingGUISettings.spriteAnimationArray != null && _assetLoadingGUISettings.spriteAnimationArray.Length > 0)
{
int currentFrameIndex = Mathf.FloorToInt(Time.time * 30f) % _assetLoadingGUISettings.spriteAnimationArray.Length;
Sprite currentFrameSprite = _assetLoadingGUISettings.spriteAnimationArray[currentFrameIndex];
Rect loadingAnimationPosRect = new Rect(Screen.width / 2 - currentFrameSprite.rect.width / 2, Screen.height / 2 - currentFrameSprite.rect.height / 2, currentFrameSprite.rect.width, currentFrameSprite.rect.height);
Rect textureUVRect = new Rect(currentFrameSprite.rect.x / currentFrameSprite.texture.width, currentFrameSprite.rect.y / currentFrameSprite.texture.height, currentFrameSprite.rect.width / currentFrameSprite.texture.width, currentFrameSprite.rect.height / currentFrameSprite.texture.height);
GUI.DrawTextureWithTexCoords(loadingAnimationPosRect, currentFrameSprite.texture, textureUVRect);
}
#endregion
#region Loading Status
_assetLoadingStatusString = "";
if (_assetLoadingCoroutine != null && _currentLoadingAssetCollection != null)
{
_assetLoadingStatusString += string.Format("<b>Asset Loading</b>" + Environment.NewLine + "Images: {0}" + Environment.NewLine + "Audios: {1}" + Environment.NewLine + "Videos: {2}",
_currentLoadingAssetCollection.preLoadedTextures.Count,
_currentLoadingAssetCollection.preLoadedAudioClips.Count,
_currentLoadingAssetCollection.keyToAbsVideoPaths.Count);
_assetLoadingStatusString += Environment.NewLine;
if (_currentAssetLoadingWebRequest != null)
{
_assetLoadingStatusString += string.Format("<color=blue>{0}</color> --- {1}", _currentAssetLoadingWebRequest.url, _currentAssetLoadingWebRequest.downloadProgress);
}
}
GUI.Label(new Rect(0, 0, Screen.width, Screen.height), _assetLoadingStatusString, _loadingStatusGUIStyle);
#endregion
}
}
}
}