Tanoda
LoadModelFromFilePickerSample.cs
Go to the documentation of this file.
1#pragma warning disable 649
2using TriLibCore.Extensions;
3using UnityEngine;
4using UnityEngine.UI;
5
6namespace TriLibCore.Samples
7{
11 public class LoadModelFromFilePickerSample : MonoBehaviour
12 {
16 private GameObject _loadedGameObject;
17
21 [SerializeField] private Button _loadModelButton;
22
26 [SerializeField] private Text _progressText;
27
35 public void LoadModel()
36 {
37 var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
38 var assetLoaderFilePicker = AssetLoaderFilePicker.Create();
39 assetLoaderFilePicker.LoadModelFromFilePickerAsync("Select a Model file", OnLoad, OnMaterialsLoad,
40 OnProgress, OnBeginLoad, OnError, null, assetLoaderOptions);
41 }
42
47 private void OnBeginLoad(bool filesSelected)
48 {
49 _loadModelButton.interactable = !filesSelected;
50 _progressText.enabled = filesSelected;
51 }
52
60 private void OnError(IContextualizedError obj)
61 {
62 Debug.LogError($"An error ocurred while loading your Model: {obj.GetInnerException()}");
63 }
64
70 private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
71 {
72 _progressText.text = $"Progress: {progress:P}";
73 }
74
80 private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
81 {
82 if (assetLoaderContext.RootGameObject != null)
83 Debug.Log("Model fully loaded.");
84 else
85 Debug.Log("Model could not be loaded.");
86 _loadModelButton.interactable = true;
87 _progressText.enabled = false;
88 }
89
95 private void OnLoad(AssetLoaderContext assetLoaderContext)
96 {
97 if (_loadedGameObject != null) Destroy(_loadedGameObject);
98 _loadedGameObject = assetLoaderContext.RootGameObject;
99 if (_loadedGameObject != null) Camera.main.FitToBounds(assetLoaderContext.RootGameObject, 2f);
100 }
101 }
102}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Represents an Asset Loader which loads files using a platform-specific file picker.
static AssetLoaderFilePicker Create()
Creates the Asset Loader File Picker Singleton instance.
void LoadModelFromFilePickerAsync(string title, Action< AssetLoaderContext > onLoad, Action< AssetLoaderContext > onMaterialsLoad, Action< AssetLoaderContext, float > onProgress, Action< bool > onBeginLoad, Action< IContextualizedError > onError, GameObject wrapperGameObject, AssetLoaderOptions assetLoaderOptions)
Loads a Model from the OS file picker asynchronously, or synchronously when the OS doesn't support Th...
Represents a sample that loads a Model from a file-picker.
void LoadModel()
Creates the AssetLoaderOptions instance and displays the Model file-picker.