LoadModelFromStreamSample.cs 4.1 KB

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