ListModelResourcesSample.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #pragma warning disable 649
  2. using TriLibCore.General;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. namespace TriLibCore.Samples
  9. {
  10. /// <summary>
  11. /// Represents a sample that loads the "TriLibSample.obj" Model from the "Models" folder and lists the Model Resources.
  12. /// </summary>
  13. public class ListModelResourcesSample : MonoBehaviour
  14. {
  15. #if UNITY_EDITOR
  16. /// <summary>
  17. /// The Model asset used to locate the filename when running in Unity Editor.
  18. /// </summary>
  19. [SerializeField]
  20. private Object ModelAsset;
  21. #endif
  22. /// <summary>
  23. /// Returns the path to the "TriLibSample.obj" Model.
  24. /// </summary>
  25. private string ModelPath
  26. {
  27. get
  28. {
  29. #if UNITY_EDITOR
  30. return AssetDatabase.GetAssetPath(ModelAsset);
  31. #else
  32. return "Models/TriLibSampleModel.obj";
  33. #endif
  34. }
  35. }
  36. /// <summary>
  37. /// The Text used to display the Model Resources.
  38. /// </summary>
  39. [SerializeField]
  40. private Text ResourcesText;
  41. /// <summary>
  42. /// The previously loaded GameObject, if any.
  43. /// </summary>
  44. private GameObject _loadedGameObject;
  45. /// <summary>
  46. /// Cached Asset Loader Options instance.
  47. /// </summary>
  48. private AssetLoaderOptions _assetLoaderOptions;
  49. /// <summary>
  50. /// Creates the AssetLoaderOptions instance and displays the Model file-picker.
  51. /// </summary>
  52. /// <remarks>
  53. /// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
  54. /// </remarks>
  55. public void LoadModel()
  56. {
  57. if (_assetLoaderOptions == null)
  58. {
  59. _assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(false, true);
  60. }
  61. var assetLoaderFilePicker = AssetLoaderFilePicker.Create();
  62. assetLoaderFilePicker.LoadModelFromFilePickerAsync("Select a Model file", OnLoad, OnMaterialsLoad, OnProgress, OnBeginLoad, OnError, null, _assetLoaderOptions);
  63. }
  64. /// <summary>
  65. /// Loads the "Models/TriLibSample.obj" Model using the given AssetLoaderOptions.
  66. /// </summary>
  67. /// <remarks>
  68. /// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
  69. /// </remarks>
  70. private void Start()
  71. {
  72. if (_assetLoaderOptions == null)
  73. {
  74. _assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(false, true);
  75. }
  76. AssetLoader.LoadModelFromFile(ModelPath, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, _assetLoaderOptions);
  77. }
  78. /// <summary>
  79. /// Called when the the Model begins to load, configuring the scene.
  80. /// </summary>
  81. /// <param name="filesSelected">Indicates if any file has been selected.</param>
  82. private void OnBeginLoad(bool filesSelected)
  83. {
  84. if (filesSelected)
  85. {
  86. Debug.Log($"User selected a Model.");
  87. //Destroys the previously loaded GameObject, if any.
  88. if (_loadedGameObject != null)
  89. {
  90. Destroy(_loadedGameObject);
  91. }
  92. //Resets the Resources Text and the previous loaded GameObject.
  93. ResourcesText.text = "Loading Model";
  94. _loadedGameObject = null;
  95. }
  96. }
  97. /// <summary>
  98. /// Called when any error occurs.
  99. /// </summary>
  100. /// <param name="obj">The contextualized error, containing the original exception and the context passed to the method where the error was thrown.</param>
  101. private void OnError(IContextualizedError obj)
  102. {
  103. Debug.LogError($"An error occurred while loading your Model: {obj.GetInnerException()}");
  104. }
  105. /// <summary>
  106. /// Called when the Model loading progress changes.
  107. /// </summary>
  108. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  109. /// <param name="progress">The loading progress.</param>
  110. private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
  111. {
  112. Debug.Log($"Loading Model. Progress: {progress:P}");
  113. }
  114. /// <summary>
  115. /// Called when the Model (including Textures and Materials) has been fully loaded.
  116. /// </summary>
  117. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  118. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  119. private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
  120. {
  121. Debug.Log("Materials loaded. Model fully loaded.");
  122. //The text containing all the Model Resources.
  123. var text = "";
  124. //ModelPath contains the loaded model path
  125. var modelPath = assetLoaderContext.Filename;
  126. if (!string.IsNullOrEmpty(modelPath))
  127. {
  128. text += $"Model: '{modelPath}'\n";
  129. }
  130. //Iterate the loaded textures list
  131. foreach (var kvp in assetLoaderContext.LoadedTextures)
  132. {
  133. //FinalPath contains the loaded texture filename
  134. string finalPath = kvp.Key.ResolvedFilename;
  135. if (!string.IsNullOrEmpty(finalPath))
  136. {
  137. text += $"Texture: '{finalPath}'\n";
  138. }
  139. }
  140. //Iterate the loaded resources list
  141. foreach (var kvp in assetLoaderContext.LoadedExternalData)
  142. {
  143. //FinalPath contains the loaded resource filename
  144. string finalPath = kvp.Value;
  145. if (!string.IsNullOrEmpty(finalPath))
  146. {
  147. text += $"External Data: '{finalPath}'\n";
  148. }
  149. }
  150. //Displays the Model Resources text.
  151. ResourcesText.text = text;
  152. }
  153. /// <summary>
  154. /// Called when the Model Meshes and hierarchy are loaded.
  155. /// </summary>
  156. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  157. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  158. private void OnLoad(AssetLoaderContext assetLoaderContext)
  159. {
  160. Debug.Log("Model loaded. Loading materials.");
  161. //Stores the loaded GameObject reference.
  162. _loadedGameObject = assetLoaderContext.RootGameObject;
  163. }
  164. }
  165. }