AssetDownloaderBehaviour.cs 5.7 KB

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