AssetDownloader.cs 4.6 KB

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