LoadModelFromFilePickerSample.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using TriLibCore.General;
  2. using UnityEngine;
  3. using TriLibCore.Extensions;
  4. using UnityEngine.UI;
  5. namespace TriLibCore.Samples
  6. {
  7. /// <summary>
  8. /// Represents a sample that loads a Model from a file-picker.
  9. /// </summary>
  10. public class LoadModelFromFilePickerSample : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// The last loaded GameObject.
  14. /// </summary>
  15. private GameObject _loadedGameObject;
  16. /// <summary>
  17. /// The load model Button.
  18. /// </summary>
  19. [SerializeField]
  20. private Button _loadModelButton;
  21. /// <summary>
  22. /// Creates the AssetLoaderOptions instance and displays the Model file-picker.
  23. /// </summary>
  24. /// <remarks>
  25. /// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
  26. /// </remarks>
  27. public void LoadModel()
  28. {
  29. var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  30. var assetLoaderFilePicker = AssetLoaderFilePicker.Create();
  31. assetLoaderFilePicker.LoadModelFromFilePickerAsync("Select a Model file", OnLoad, OnMaterialsLoad, OnProgress, OnBeginLoad, OnError, null, assetLoaderOptions);
  32. }
  33. /// <summary>
  34. /// Called when the the Model begins to load.
  35. /// </summary>
  36. /// <param name="filesSelected">Indicates if any file has been selected.</param>
  37. private void OnBeginLoad(bool filesSelected)
  38. {
  39. _loadModelButton.interactable = false;
  40. }
  41. /// <summary>
  42. /// Called when any error occurs.
  43. /// </summary>
  44. /// <param name="obj">The contextualized error, containing the original exception and the context passed to the method where the error was thrown.</param>
  45. private void OnError(IContextualizedError obj)
  46. {
  47. Debug.LogError($"An error ocurred while loading your Model: {obj.GetInnerException()}");
  48. }
  49. /// <summary>
  50. /// Called when the Model loading progress changes.
  51. /// </summary>
  52. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  53. /// <param name="progress">The loading progress.</param>
  54. private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
  55. {
  56. Debug.Log($"Loading Model. Progress: {progress:P}");
  57. }
  58. /// <summary>
  59. /// Called when the Model (including Textures and Materials) has been fully loaded, or after any error occurs.
  60. /// </summary>
  61. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  62. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  63. private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
  64. {
  65. if (assetLoaderContext.RootGameObject != null)
  66. {
  67. Debug.Log("Materials loaded. Model fully loaded.");
  68. }
  69. else
  70. {
  71. Debug.Log("Model could not be loaded.");
  72. }
  73. }
  74. /// <summary>
  75. /// Called when the Model Meshes and hierarchy are loaded.
  76. /// </summary>
  77. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  78. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  79. private void OnLoad(AssetLoaderContext assetLoaderContext)
  80. {
  81. if (_loadedGameObject != null)
  82. {
  83. Destroy(_loadedGameObject);
  84. }
  85. _loadedGameObject = assetLoaderContext.RootGameObject;
  86. if (_loadedGameObject != null)
  87. {
  88. Camera.main.FitToBounds(assetLoaderContext.RootGameObject, 2f);
  89. Debug.Log("Model loaded. Loading materials.");
  90. }
  91. else
  92. {
  93. Debug.Log("Model materials could not be loaded.");
  94. }
  95. _loadModelButton.interactable = true;
  96. }
  97. }
  98. }