LoadModelFromFileSample.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /// Loads the "Models/TriLibSample.obj" Model using the given AssetLoaderOptions.
  30. /// </summary>
  31. /// <remarks>
  32. /// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
  33. /// </remarks>
  34. private void Start()
  35. {
  36. var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  37. AssetLoader.LoadModelFromFile(ModelPath, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, assetLoaderOptions);
  38. }
  39. /// <summary>
  40. /// Called when any error occurs.
  41. /// </summary>
  42. /// <param name="obj">The contextualized error, containing the original exception and the context passed to the method where the error was thrown.</param>
  43. private void OnError(IContextualizedError obj)
  44. {
  45. Debug.LogError($"An error occurred while loading your Model: {obj.GetInnerException()}");
  46. }
  47. /// <summary>
  48. /// Called when the Model loading progress changes.
  49. /// </summary>
  50. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  51. /// <param name="progress">The loading progress.</param>
  52. private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
  53. {
  54. Debug.Log($"Loading Model. Progress: {progress:P}");
  55. }
  56. /// <summary>
  57. /// Called when the Model (including Textures and Materials) has been fully loaded.
  58. /// </summary>
  59. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  60. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  61. private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
  62. {
  63. Debug.Log("Materials loaded. Model fully loaded.");
  64. }
  65. /// <summary>
  66. /// Called when the Model Meshes and hierarchy are loaded.
  67. /// </summary>
  68. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  69. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  70. private void OnLoad(AssetLoaderContext assetLoaderContext)
  71. {
  72. Debug.Log("Model loaded. Loading materials.");
  73. }
  74. }
  75. }