Tanoda
AssetDownloaderBehaviour.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.IO;
4using TriLibCore.General;
5using TriLibCore.Utils;
6using UnityEngine;
7using UnityEngine.Networking;
8
9namespace TriLibCore
10{
12 public class AssetDownloaderBehaviour : MonoBehaviour
13 {
17 private UnityWebRequest _unityWebRequest;
18
22 private Action<AssetLoaderContext, float> _onProgress;
23
27 private AssetLoaderContext _assetLoaderContext;
28
41 public IEnumerator DownloadAsset(UnityWebRequest unityWebRequest, Action<AssetLoaderContext> onLoad, Action<AssetLoaderContext> onMaterialsLoad, Action<AssetLoaderContext, float> onProgress, GameObject wrapperGameObject, Action<IContextualizedError> onError, AssetLoaderOptions assetLoaderOptions, object customContextData, string fileExtension, bool? isZipFile = null)
42 {
43 _unityWebRequest = unityWebRequest;
44 _onProgress = onProgress;
45 yield return unityWebRequest.SendWebRequest();
46 try
47 {
48 if (unityWebRequest.responseCode < 400)
49 {
50 var memoryStream = new MemoryStream(_unityWebRequest.downloadHandler.data);
51 var uriLoadCustomContextData = new UriLoadCustomContextData
52 {
53 UnityWebRequest = _unityWebRequest,
54 CustomData = customContextData
55 };
56 var contentType = unityWebRequest.GetResponseHeader("Content-Type");
57 if (contentType != null && isZipFile == null)
58 {
59 isZipFile = contentType.Contains("application/zip") || contentType.Contains("application/x-zip-compressed") || contentType.Contains("multipart/x-zip");
60 }
61 if (!isZipFile.GetValueOrDefault() && string.IsNullOrWhiteSpace(fileExtension))
62 {
63 fileExtension = FileUtils.GetFileExtension(unityWebRequest.url);
64 }
65 if (isZipFile.GetValueOrDefault())
66 {
67 _assetLoaderContext = AssetLoaderZip.LoadModelFromZipStream(memoryStream, onLoad, onMaterialsLoad, delegate (AssetLoaderContext assetLoaderContext, float progress) { onProgress?.Invoke(assetLoaderContext, 0.5f + progress * 0.5f); }, onError, wrapperGameObject, assetLoaderOptions, uriLoadCustomContextData, fileExtension);
68 }
69 else
70 {
71 _assetLoaderContext = AssetLoader.LoadModelFromStream(memoryStream, null, fileExtension, onLoad, onMaterialsLoad, delegate (AssetLoaderContext assetLoaderContext, float progress) { onProgress?.Invoke(assetLoaderContext, 0.5f + progress * 0.5f); }, onError, wrapperGameObject, assetLoaderOptions, uriLoadCustomContextData);
72 }
73 }
74 else
75 {
76 var exception = new Exception($"UnityWebRequest error:{unityWebRequest.error}, code:{unityWebRequest.responseCode}");
77 throw exception;
78 }
79 }
80 catch (Exception exception)
81 {
82 if (onError != null)
83 {
84 var contextualizedError = exception as IContextualizedError;
85 onError(contextualizedError ?? new ContextualizedError<AssetLoaderContext>(exception, null));
86 }
87 else
88 {
89 throw;
90 }
91 }
92 Destroy(gameObject);
93 }
94
96 private void Update()
97 {
98 _onProgress?.Invoke(_assetLoaderContext, _unityWebRequest.downloadProgress * 0.5F);
99 }
100 }
101}
TriLibCore.AssetLoaderOptions AssetLoaderOptions
Definition: TriLibLoader.cs:9
Represents a class used to download Models with Coroutines used by the Asset Downloader.
IEnumerator DownloadAsset(UnityWebRequest unityWebRequest, Action< AssetLoaderContext > onLoad, Action< AssetLoaderContext > onMaterialsLoad, Action< AssetLoaderContext, float > onProgress, GameObject wrapperGameObject, Action< IContextualizedError > onError, AssetLoaderOptions assetLoaderOptions, object customContextData, string fileExtension, bool? isZipFile=null)
Downloads the Model using the given Request and options.
Represents a class passed as the custom data to the Asset Loader Context when loading Models from URI...