LoadModelFromStreamSample.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.IO;
  2. using TriLibCore.General;
  3. using TriLibCore.Mappers;
  4. using UnityEngine;
  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
  12. /// using a File Stream and sample Mappers to find external Resources and Textures.
  13. /// </summary>
  14. public class LoadModelFromStreamSample : MonoBehaviour
  15. {
  16. #if UNITY_EDITOR
  17. /// <summary>
  18. /// The Model asset used to locate the filename when running in Unity Editor.
  19. /// </summary>
  20. [SerializeField]
  21. private Object ModelAsset;
  22. #endif
  23. /// <summary>
  24. /// Returns the path to the "TriLibSample.obj" Model.
  25. /// </summary>
  26. private string ModelPath
  27. {
  28. get
  29. {
  30. #if UNITY_EDITOR
  31. return AssetDatabase.GetAssetPath(ModelAsset);
  32. #else
  33. return "Models/TriLibSampleModel.obj";
  34. #endif
  35. }
  36. }
  37. /// <summary>
  38. /// Creates the AssetLoaderOptions instance, adds the sample Mappers to it and loads the Model from a File Stream.
  39. /// </summary>
  40. /// <remarks>
  41. /// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
  42. /// You can configure the Mappers straight into the AssetLoaderOptions instance in the Editor.
  43. /// You can create Mapper assets from existing Mapper Scripts by right-clicking them on the Assets Explorer and selecting "Create Mapper Instance".
  44. /// </remarks>
  45. private void Start()
  46. {
  47. var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  48. assetLoaderOptions.ExternalDataMapper = ScriptableObject.CreateInstance<ExternalDataMapperSample>();
  49. assetLoaderOptions.TextureMapper = ScriptableObject.CreateInstance<TextureMapperSample>();
  50. AssetLoader.LoadModelFromStream(File.OpenRead(ModelPath), ModelPath, null, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, assetLoaderOptions);
  51. }
  52. /// <summary>
  53. /// Called when any error occurs.
  54. /// </summary>
  55. /// <param name="obj">The contextualized error, containing the original exception and the context passed to the method where the error was thrown.</param>
  56. private void OnError(IContextualizedError obj)
  57. {
  58. Debug.LogError($"An error ocurred while loading your Model: {obj.GetInnerException()}");
  59. }
  60. /// <summary>
  61. /// Called when the Model loading progress changes.
  62. /// </summary>
  63. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  64. /// <param name="progress">The loading progress.</param>
  65. private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
  66. {
  67. Debug.Log($"Loading Model. Progress: {progress:P}");
  68. }
  69. /// <summary>
  70. /// Called when the Model (including Textures and Materials) has been fully loaded, or after any error occurs.
  71. /// </summary>
  72. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  73. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  74. private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
  75. {
  76. Debug.Log("Materials loaded. Model fully loaded.");
  77. }
  78. /// <summary>
  79. /// Called when the Model Meshes and hierarchy are loaded.
  80. /// </summary>
  81. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  82. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  83. private void OnLoad(AssetLoaderContext assetLoaderContext)
  84. {
  85. Debug.Log("Model loaded. Loading materials.");
  86. }
  87. }
  88. }