using System;
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:
#if UNITY_2023_1_OR_NEWER
unityWebRequest = UnityWebRequest.PostWwwForm(uri, data);
#else
unityWebRequest = UnityWebRequest.PostWwwForm(uri, data);
#endif
break;
case HttpRequestMethod.Put:
unityWebRequest = UnityWebRequest.Put(uri, data);
break;
case HttpRequestMethod.Delete:
unityWebRequest = UnityWebRequest.Delete(data != null ? $"{uri}?{data}" : uri);
break;
case HttpRequestMethod.Head:
unityWebRequest = UnityWebRequest.Head(data != null ? $"{uri}?{data}" : uri);
break;
default:
unityWebRequest = UnityWebRequest.Get(data != null ? $"{uri}?{data}" : uri);
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 is loaded but resources may still pending.
/// The Method to call on the Main Thread when the Model and resources are 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 options to use when loading the Model.
/// The Custom Data that will be passed along the Context.
/// The extension of the URI Model or the Model inside the Zip file.
/// Pass true if your file is a Zip file.
/// Turn on this field to avoid loading the model immediately and chain the Tasks.
/// 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, bool haltTask = false)
{
var assetDownloader = new GameObject("Asset Downloader").AddComponent();
return assetDownloader.StartCoroutine(assetDownloader.DownloadAsset(unityWebRequest, onLoad, onMaterialsLoad, onProgress, wrapperGameObject, onError, assetLoaderOptions, customContextData, fileExtension, isZipFile));
}
}
}