#pragma warning disable 649
using TriLibCore.General;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace TriLibCore.Samples
{
///
/// Represents a sample that loads the "TriLibSample.obj" Model from the "Models" folder and lists the Model Resources.
///
public class ListModelResourcesSample : MonoBehaviour
{
#if UNITY_EDITOR
///
/// The Model asset used to locate the filename when running in Unity Editor.
///
[SerializeField]
private Object ModelAsset;
#endif
///
/// Returns the path to the "TriLibSample.obj" Model.
///
private string ModelPath
{
get
{
#if UNITY_EDITOR
return AssetDatabase.GetAssetPath(ModelAsset);
#else
return "Models/TriLibSampleModel.obj";
#endif
}
}
///
/// The Text used to display the Model Resources.
///
[SerializeField]
private Text ResourcesText;
///
/// The previously loaded GameObject, if any.
///
private GameObject _loadedGameObject;
///
/// Cached Asset Loader Options instance.
///
private AssetLoaderOptions _assetLoaderOptions;
///
/// Creates the AssetLoaderOptions instance and displays the Model file-picker.
///
///
/// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
///
public void LoadModel()
{
if (_assetLoaderOptions == null)
{
_assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(false, true);
}
var assetLoaderFilePicker = AssetLoaderFilePicker.Create();
assetLoaderFilePicker.LoadModelFromFilePickerAsync("Select a Model file", OnLoad, OnMaterialsLoad, OnProgress, OnBeginLoad, OnError, null, _assetLoaderOptions);
}
///
/// Loads the "Models/TriLibSample.obj" Model using the given AssetLoaderOptions.
///
///
/// 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)
{
_assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(false, true);
}
AssetLoader.LoadModelFromFile(ModelPath, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, _assetLoaderOptions);
}
///
/// Called when the the Model begins to load, configuring the scene.
///
/// Indicates if any file has been selected.
private void OnBeginLoad(bool filesSelected)
{
if (filesSelected)
{
Debug.Log($"User selected a Model.");
//Destroys the previously loaded GameObject, if any.
if (_loadedGameObject != null)
{
Destroy(_loadedGameObject);
}
//Resets the Resources Text and the previous loaded GameObject.
ResourcesText.text = "Loading Model";
_loadedGameObject = null;
}
}
///
/// 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.");
//The text containing all the Model Resources.
var text = "";
//ModelPath contains the loaded model path
var modelPath = assetLoaderContext.Filename;
if (!string.IsNullOrEmpty(modelPath))
{
text += $"Model: '{modelPath}'\n";
}
//Iterate the loaded textures list
foreach (var kvp in assetLoaderContext.LoadedTextures)
{
//FinalPath contains the loaded texture filename
string finalPath = kvp.Key.ResolvedFilename;
if (!string.IsNullOrEmpty(finalPath))
{
text += $"Texture: '{finalPath}'\n";
}
}
//Iterate the loaded resources list
foreach (var kvp in assetLoaderContext.LoadedExternalData)
{
//FinalPath contains the loaded resource filename
string finalPath = kvp.Value;
if (!string.IsNullOrEmpty(finalPath))
{
text += $"External Data: '{finalPath}'\n";
}
}
//Displays the Model Resources text.
ResourcesText.text = text;
}
///
/// 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.");
//Stores the loaded GameObject reference.
_loadedGameObject = assetLoaderContext.RootGameObject;
}
}
}