AssetDownloaderBehaviour.cs 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Net;
  5. using System.Threading;
  6. using TriLibCore.General;
  7. using UnityEngine;
  8. using UnityEngine.Networking;
  9. namespace TriLibCore
  10. {
  11. /// <summary>Represents a class used to download Models with Coroutines used by the Asset Downloader.</summary>
  12. public class AssetDownloaderBehaviour : MonoBehaviour
  13. {
  14. /// <summary>
  15. /// Unity web request instance used on this script.
  16. /// </summary>
  17. private UnityWebRequest _unityWebRequest;
  18. /// <summary>
  19. /// Method to call when the model downloading progress changes.
  20. /// </summary>
  21. private Action<AssetLoaderContext, float> _onProgress;
  22. /// <summary>
  23. /// Context used to load the model.
  24. /// </summary>
  25. private AssetLoaderContext _assetLoaderContext;
  26. /// <summary>Downloads the Model using the given Request and options.</summary>
  27. /// <param name="unityWebRequest">The Unity Web Request used to load the Model. You can use the CreateWebRequest method to create a new Unity Web Request or pass your instance.</param>
  28. /// <param name="onLoad">The Method to call on the Main Thread when the Model Meshes and hierarchy are loaded.</param>
  29. /// <param name="onMaterialsLoad">The Method to call on the Main Thread when the Model (including Textures and Materials) has been fully loaded.</param>
  30. /// <param name="onProgress">The Method to call when the Model loading progress changes.</param>
  31. /// <param name="wrapperGameObject">The Game Object that will be the parent of the loaded Game Object. Can be null.</param>
  32. /// <param name="onError">The Method to call on the Main Thread when any error occurs.</param>
  33. /// <param name="assetLoaderOptions">The Asset Loader Options reference. Asset Loader Options contains various options used during the Model loading process.</param>
  34. /// <param name="customContextData">The Custom Data that will be passed along the AssetLoaderContext.</param>
  35. /// <param name="fileExtension">The extension of the URI Model.</param>
  36. /// <param name="isZipFile">Pass <c>true</c> if your file is a Zip file.</param>
  37. /// <returns>The download coroutine enumerator.</returns>
  38. 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)
  39. {
  40. _unityWebRequest = unityWebRequest;
  41. _onProgress = onProgress;
  42. yield return unityWebRequest.SendWebRequest();
  43. if (unityWebRequest.responseCode < 400)
  44. {
  45. var memoryStream = new MemoryStream(_unityWebRequest.downloadHandler.data);
  46. var uriLoadCustomContextData = new UriLoadCustomContextData
  47. {
  48. UnityWebRequest = _unityWebRequest,
  49. CustomData = customContextData
  50. };
  51. var contentType = unityWebRequest.GetResponseHeader("Content-Type");
  52. if (contentType != null && isZipFile == null)
  53. {
  54. isZipFile = contentType.Contains("application/zip") || contentType.Contains("application/x-zip-compressed") || contentType.Contains("multipart/x-zip");
  55. }
  56. if (isZipFile.GetValueOrDefault())
  57. {
  58. _assetLoaderContext = AssetLoaderZip.LoadModelFromZipStream(memoryStream, onLoad, onMaterialsLoad, delegate (AssetLoaderContext assetLoaderContext, float progress) { onProgress?.Invoke(assetLoaderContext, 0.5f + progress * 0.5f); }, onError, wrapperGameObject, assetLoaderOptions, uriLoadCustomContextData, fileExtension);
  59. }
  60. else
  61. {
  62. _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);
  63. }
  64. }
  65. else
  66. {
  67. var exception = new Exception($"UnityWebRequest error:{unityWebRequest.error}, code:{unityWebRequest.responseCode}");
  68. if (onError != null)
  69. {
  70. var contextualizedError = exception as IContextualizedError;
  71. onError(contextualizedError ?? new ContextualizedError<AssetLoaderContext>(exception, null));
  72. }
  73. else
  74. {
  75. throw exception;
  76. }
  77. }
  78. Destroy(gameObject);
  79. }
  80. /// <summary>Updates the download progress.</summary>
  81. private void Update()
  82. {
  83. _onProgress?.Invoke(_assetLoaderContext, _unityWebRequest.downloadProgress * 0.5F);
  84. }
  85. }
  86. }