AssetDownloader.cs 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Networking;
  4. namespace TriLibCore
  5. {
  6. /// <summary>Represents a class to download and load Models.</summary>
  7. public class AssetDownloader
  8. {
  9. /// <summary>Represents an HTTP Request method.</summary>
  10. public enum HttpRequestMethod
  11. {
  12. /// <summary>The HTTP GET method.</summary>
  13. Get,
  14. /// <summary>The HTTP POST method.</summary>
  15. Post,
  16. /// <summary>The HTTP PUT method.</summary>
  17. Put,
  18. /// <summary>The HTTP DELETE method.</summary>
  19. Delete,
  20. /// <summary>The HTTP HEAD method.</summary>
  21. Head
  22. }
  23. /// <summary>Creates a Unity Web Request from the given parameters.</summary>
  24. /// <param name="uri">The Request URI (URL).</param>
  25. /// <param name="httpRequestMethod">The HTTP Request method to use.</param>
  26. /// <param name="data">The Custom Data that was sent along the Request.</param>
  27. /// <param name="timeout">The Request timeout in seconds).</param>
  28. /// <returns>The created unity web request.</returns>
  29. public static UnityWebRequest CreateWebRequest(string uri, HttpRequestMethod httpRequestMethod = HttpRequestMethod.Get, string data = null, int timeout = 2000)
  30. {
  31. UnityWebRequest unityWebRequest;
  32. switch (httpRequestMethod)
  33. {
  34. case HttpRequestMethod.Post:
  35. #if UNITY_2023_1_OR_NEWER
  36. unityWebRequest = UnityWebRequest.PostWwwForm(uri, data);
  37. #else
  38. unityWebRequest = UnityWebRequest.PostWwwForm(uri, data);
  39. #endif
  40. break;
  41. case HttpRequestMethod.Put:
  42. unityWebRequest = UnityWebRequest.Put(uri, data);
  43. break;
  44. case HttpRequestMethod.Delete:
  45. unityWebRequest = UnityWebRequest.Delete(data != null ? $"{uri}?{data}" : uri);
  46. break;
  47. case HttpRequestMethod.Head:
  48. unityWebRequest = UnityWebRequest.Head(data != null ? $"{uri}?{data}" : uri);
  49. break;
  50. default:
  51. unityWebRequest = UnityWebRequest.Get(data != null ? $"{uri}?{data}" : uri);
  52. break;
  53. }
  54. unityWebRequest.timeout = timeout;
  55. return unityWebRequest;
  56. }
  57. /// <summary>Loads a Model from the given URI Asynchronously (Accepts zip files).</summary>
  58. /// <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>
  59. /// <param name="onLoad">The Method to call on the Main Thread when the Model is loaded but resources may still pending.</param>
  60. /// <param name="onMaterialsLoad">The Method to call on the Main Thread when the Model and resources are loaded.</param>
  61. /// <param name="onProgress">The Method to call when the Model loading progress changes.</param>
  62. /// <param name="onError">The Method to call on the Main Thread when any error occurs.</param>
  63. /// <param name="wrapperGameObject">The Game Object that will be the parent of the loaded Game Object. Can be null.</param>
  64. /// <param name="assetLoaderOptions">The options to use when loading the Model.</param>
  65. /// <param name="customContextData">The Custom Data that will be passed along the Context.</param>
  66. /// <param name="fileExtension">The extension of the URI Model or the Model inside the Zip file.</param>
  67. /// <param name="isZipFile">Pass <c>true</c> if your file is a Zip file.</param>
  68. /// <param name="haltTask">Turn on this field to avoid loading the model immediately and chain the Tasks.</param>
  69. /// <returns>The AssetLoaderContext used to load the model.</returns>
  70. public static Coroutine LoadModelFromUri(UnityWebRequest unityWebRequest, Action<AssetLoaderContext> onLoad, Action<AssetLoaderContext> onMaterialsLoad, Action<AssetLoaderContext, float> onProgress, Action<IContextualizedError> onError = null, GameObject wrapperGameObject = null, AssetLoaderOptions assetLoaderOptions = null, object customContextData = null, string fileExtension = null, bool? isZipFile = null, bool haltTask = false)
  71. {
  72. var assetDownloader = new GameObject("Asset Downloader").AddComponent<AssetDownloaderBehaviour>();
  73. return assetDownloader.StartCoroutine(assetDownloader.DownloadAsset(unityWebRequest, onLoad, onMaterialsLoad, onProgress, wrapperGameObject, onError, assetLoaderOptions, customContextData, fileExtension, isZipFile));
  74. }
  75. }
  76. }