#pragma warning disable 649
using System.IO;
using TriLibCore.General;
using TriLibCore.Mappers;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace TriLibCore.Samples
{
///
/// Represents a sample that loads the "TriLibSample.obj" Model from the "Models" folder
/// using a File Stream and sample Mappers to find external Resources and Textures.
///
public class LoadModelFromStreamSample : MonoBehaviour
{
///
/// Cached Asset Loader Options instance.
///
private AssetLoaderOptions _assetLoaderOptions;
///
/// Returns the path to the "TriLibSample.obj" Model.
///
private string ModelPath
{
get
{
#if UNITY_EDITOR
return $"{Application.dataPath}/TriLib/TriLibSamples/LoadModelFromStream/Models/TriLibSampleModel.obj";
#else
return "Models/TriLibSampleModel.obj";
#endif
}
}
///
/// Creates the AssetLoaderOptions instance, adds the sample Mappers to it and loads the Model from a File Stream.
///
///
/// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
/// You can configure the Mappers straight into the AssetLoaderOptions instance in the Editor.
/// You can create Mapper assets from existing Mapper Scripts by right-clicking them on the Assets Explorer and selecting "Create Mapper Instance".
///
private void Start()
{
if (_assetLoaderOptions == null)
{
_assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(false, true);
_assetLoaderOptions.ExternalDataMapper = ScriptableObject.CreateInstance();
_assetLoaderOptions.TextureMappers = new TextureMapper[] { ScriptableObject.CreateInstance() };
}
AssetLoader.LoadModelFromStream(File.OpenRead(ModelPath), ModelPath, null, 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.");
}
}
}