using System; using System.Threading; using TriLibCore.General; using UnityEngine; using UnityEngine.Networking; namespace TriLibCore { /// Represents a class to download and load Models. public class AssetDownloader { /// Represents an HTTP Request method. public enum HttpRequestMethod { /// The HTTP GET method. Get, /// The HTTP POST method. Post, /// The HTTP PUT method. Put, /// The HTTP DELETE method. Delete, /// The HTTP HEAD method. Head } /// Creates a Unity Web Request from the given parameters. /// The Request URI (URL). /// The HTTP Request method to use. /// The Custom Data that was sent along the Request. /// The Request timeout in seconds). /// The created unity web request. public static UnityWebRequest CreateWebRequest(string uri, HttpRequestMethod httpRequestMethod = HttpRequestMethod.Get, string data = null, int timeout = 2000) { UnityWebRequest unityWebRequest; switch (httpRequestMethod) { case HttpRequestMethod.Post: unityWebRequest = UnityWebRequest.Post(uri, data); break; case HttpRequestMethod.Put: unityWebRequest = UnityWebRequest.Put(uri, data); break; case HttpRequestMethod.Delete: unityWebRequest = UnityWebRequest.Delete($"{uri}?{data}"); break; case HttpRequestMethod.Head: unityWebRequest = UnityWebRequest.Head($"{uri}?{data}"); break; default: unityWebRequest = UnityWebRequest.Get($"{uri}?{data}"); break; } unityWebRequest.timeout = timeout; return unityWebRequest; } /// Loads a Model from the given URI Asynchronously (Accepts zip files). /// 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. /// The Method to call on the Main Thread when the Model Meshes and hierarchy are loaded. /// The Method to call on the Main Thread when the Model (including Textures and Materials) has been fully loaded. /// The Method to call when the Model loading progress changes. /// The Method to call on the Main Thread when any error occurs. /// The Game Object that will be the parent of the loaded Game Object. Can be null. /// The Asset Loader Options reference. Asset Loader Options contains various options used during the Model loading process. /// The Custom Data that will be passed along the AssetLoaderContext. /// The extension of the URI Model or the Model inside the Zip file. /// Pass true if your file is a Zip file. /// The AssetLoaderContext used to load the model. public static Coroutine LoadModelFromUri(UnityWebRequest unityWebRequest, Action onLoad, Action onMaterialsLoad, Action onProgress, Action onError = null, GameObject wrapperGameObject = null, AssetLoaderOptions assetLoaderOptions = null, object customContextData = null, string fileExtension = null, bool? isZipFile = null) { var assetDownloader = new GameObject("Asset Downloader").AddComponent(); return assetDownloader.StartCoroutine(assetDownloader.DownloadAsset(unityWebRequest, onLoad, onMaterialsLoad, onProgress, wrapperGameObject, onError, assetLoaderOptions, customContextData, fileExtension, isZipFile)); } } }