LoadModelFromFilePickerSample.cs 4.5 KB

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