using UnityEngine;
namespace TriLibCore.Samples
{
///
/// Represents a sample that loads a compressed (Zipped) Model.
///
public class LoadModelFromURLSample : MonoBehaviour
{
///
/// The Model URL.
///
public string ModelURL = "https://ricardoreis.net/trilib/demos/sample/TriLibSampleModel.zip";
///
/// Cached Asset Loader Options instance.
///
private AssetLoaderOptions _assetLoaderOptions;
///
/// Creates the AssetLoaderOptions instance, configures the Web Request, and downloads the Model.
///
///
/// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
///
private void Start()
{
if (_assetLoaderOptions == null)
{
var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(false, true);
}
var webRequest = AssetDownloader.CreateWebRequest(ModelURL);
AssetDownloader.LoadModelFromUri(webRequest, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, _assetLoaderOptions);
}
///
/// Called when any error occurs.
///
/// The contextualized error, containing the original exception and the context passed to the method where the error was thrown.
private void OnError(IContextualizedError obj)
{
Debug.LogError($"An error occurred while loading your Model: {obj.GetInnerException()}");
}
///
/// Called when the Model loading progress changes.
///
/// The context used to load the Model.
/// The loading progress.
private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
{
Debug.Log($"Loading Model. Progress: {progress:P}");
}
///
/// Called when the Model (including Textures and Materials) has been fully loaded.
///
/// The loaded GameObject is available on the assetLoaderContext.RootGameObject field.
/// The context used to load the Model.
private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
{
Debug.Log("Materials loaded. Model fully loaded.");
}
///
/// Called when the Model Meshes and hierarchy are loaded.
///
/// The loaded GameObject is available on the assetLoaderContext.RootGameObject field.
/// The context used to load the Model.
private void OnLoad(AssetLoaderContext assetLoaderContext)
{
Debug.Log("Model loaded. Loading materials.");
}
}
}