123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using TriLibCore.General;
- using UnityEngine;
- using TriLibCore.Extensions;
- using UnityEngine.UI;
- namespace TriLibCore.Samples
- {
- /// <summary>
- /// Represents a sample that loads a Model from a file-picker.
- /// </summary>
- public class LoadModelFromFilePickerSample : MonoBehaviour
- {
- /// <summary>
- /// The last loaded GameObject.
- /// </summary>
- private GameObject _loadedGameObject;
- /// <summary>
- /// The load model Button.
- /// </summary>
- [SerializeField]
- private Button _loadModelButton;
- /// <summary>
- /// Creates the AssetLoaderOptions instance and displays the Model file-picker.
- /// </summary>
- /// <remarks>
- /// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
- /// </remarks>
- public void LoadModel()
- {
- var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
- var assetLoaderFilePicker = AssetLoaderFilePicker.Create();
- assetLoaderFilePicker.LoadModelFromFilePickerAsync("Select a Model file", OnLoad, OnMaterialsLoad, OnProgress, OnBeginLoad, OnError, null, assetLoaderOptions);
- }
- /// <summary>
- /// Called when the the Model begins to load.
- /// </summary>
- /// <param name="filesSelected">Indicates if any file has been selected.</param>
- private void OnBeginLoad(bool filesSelected)
- {
- _loadModelButton.interactable = false;
- }
- /// <summary>
- /// Called when any error occurs.
- /// </summary>
- /// <param name="obj">The contextualized error, containing the original exception and the context passed to the method where the error was thrown.</param>
- private void OnError(IContextualizedError obj)
- {
- Debug.LogError($"An error ocurred while loading your Model: {obj.GetInnerException()}");
- }
- /// <summary>
- /// Called when the Model loading progress changes.
- /// </summary>
- /// <param name="assetLoaderContext">The context used to load the Model.</param>
- /// <param name="progress">The loading progress.</param>
- private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
- {
- Debug.Log($"Loading Model. Progress: {progress:P}");
- }
- /// <summary>
- /// Called when the Model (including Textures and Materials) has been fully loaded, or after any error occurs.
- /// </summary>
- /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
- /// <param name="assetLoaderContext">The context used to load the Model.</param>
- private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
- {
- if (assetLoaderContext.RootGameObject != null)
- {
- Debug.Log("Materials loaded. Model fully loaded.");
- }
- else
- {
- Debug.Log("Model could not be loaded.");
- }
- }
- /// <summary>
- /// Called when the Model Meshes and hierarchy are loaded.
- /// </summary>
- /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
- /// <param name="assetLoaderContext">The context used to load the Model.</param>
- private void OnLoad(AssetLoaderContext assetLoaderContext)
- {
- if (_loadedGameObject != null)
- {
- Destroy(_loadedGameObject);
- }
- _loadedGameObject = assetLoaderContext.RootGameObject;
- if (_loadedGameObject != null)
- {
- Camera.main.FitToBounds(assetLoaderContext.RootGameObject, 2f);
- Debug.Log("Model loaded. Loading materials.");
- }
- else
- {
- Debug.Log("Model materials could not be loaded.");
- }
- _loadModelButton.interactable = true;
- }
- }
- }
|