LoadModelFromFileSample.cs 3.4 KB

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