LoadModelFromFileSample.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using TriLibCore.General;
  2. using UnityEngine;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. namespace TriLibCore.Samples
  7. {
  8. /// <summary>
  9. /// Represents a sample that loads the "TriLibSample.obj" Model from the "Models" folder.
  10. /// </summary>
  11. public class LoadModelFromFileSample : MonoBehaviour
  12. {
  13. #if UNITY_EDITOR
  14. /// <summary>
  15. /// The Model asset used to locate the filename when running in Unity Editor.
  16. /// </summary>
  17. [SerializeField]
  18. private Object ModelAsset;
  19. #endif
  20. /// <summary>
  21. /// Returns the path to the "TriLibSample.obj" Model.
  22. /// </summary>
  23. private string ModelPath
  24. {
  25. get
  26. {
  27. #if UNITY_EDITOR
  28. return AssetDatabase.GetAssetPath(ModelAsset);
  29. #else
  30. return "Models/TriLibSampleModel.obj";
  31. #endif
  32. }
  33. }
  34. /// <summary>
  35. /// Loads the "Models/TriLibSample.obj" Model using the given AssetLoaderOptions.
  36. /// </summary>
  37. /// <remarks>
  38. /// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
  39. /// </remarks>
  40. private void Start()
  41. {
  42. var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  43. AssetLoader.LoadModelFromFile(ModelPath, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, assetLoaderOptions);
  44. }
  45. /// <summary>
  46. /// Called when any error occurs.
  47. /// </summary>
  48. /// <param name="obj">The contextualized error, containing the original exception and the context passed to the method where the error was thrown.</param>
  49. private void OnError(IContextualizedError obj)
  50. {
  51. Debug.LogError($"An error ocurred while loading your Model: {obj.GetInnerException()}");
  52. }
  53. /// <summary>
  54. /// Called when the Model loading progress changes.
  55. /// </summary>
  56. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  57. /// <param name="progress">The loading progress.</param>
  58. private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
  59. {
  60. Debug.Log($"Loading Model. Progress: {progress:P}");
  61. }
  62. /// <summary>
  63. /// Called when the Model (including Textures and Materials) has been fully loaded, or after any error occurs.
  64. /// </summary>
  65. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  66. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  67. private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
  68. {
  69. Debug.Log("Materials loaded. Model fully loaded.");
  70. }
  71. /// <summary>
  72. /// Called when the Model Meshes and hierarchy are loaded.
  73. /// </summary>
  74. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  75. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  76. private void OnLoad(AssetLoaderContext assetLoaderContext)
  77. {
  78. Debug.Log("Model loaded. Loading materials.");
  79. }
  80. }
  81. }