SimpleCustomAssetLoader.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma warning disable 672
  2. using System;
  3. using System.IO;
  4. using TriLibCore.Interfaces;
  5. using TriLibCore.Mappers;
  6. using TriLibCore.Utils;
  7. using UnityEngine;
  8. namespace TriLibCore.Samples
  9. {
  10. /// <summary>
  11. /// Custom external data mapper which works with callbacks.
  12. /// </summary>
  13. public class SimpleExternalDataMapper : ExternalDataMapper
  14. {
  15. private Func<string, Stream> _streamReceivingCallback;
  16. private Func<string, string> _finalPathReceivingCallback;
  17. public void Setup(Func<string, Stream> streamReceivingCallback, Func<string, string> finalPathReceivingCallback)
  18. {
  19. if (streamReceivingCallback == null)
  20. {
  21. throw new Exception("Callback parameter is missing.");
  22. }
  23. _streamReceivingCallback = streamReceivingCallback;
  24. _finalPathReceivingCallback = finalPathReceivingCallback;
  25. }
  26. public override Stream Map(AssetLoaderContext assetLoaderContext, string originalFilename, out string finalPath)
  27. {
  28. finalPath = _finalPathReceivingCallback != null ? _finalPathReceivingCallback(originalFilename) : originalFilename;
  29. return _streamReceivingCallback(originalFilename);
  30. }
  31. }
  32. /// <summary>
  33. /// Custom texture mapper which works with callbacks.
  34. /// </summary>
  35. public class SimpleTextureMapper : TextureMapper
  36. {
  37. private Func<ITexture, Stream> _streamReceivingCallback;
  38. public void Setup(Func<ITexture, Stream> streamReceivingCallback)
  39. {
  40. if (streamReceivingCallback == null)
  41. {
  42. throw new Exception("Callback parameter is missing.");
  43. }
  44. _streamReceivingCallback = streamReceivingCallback;
  45. }
  46. public override void Map(TextureLoadingContext textureLoadingContext)
  47. {
  48. var stream = _streamReceivingCallback(textureLoadingContext.Texture);
  49. textureLoadingContext.Stream = stream;
  50. }
  51. }
  52. /// <summary>
  53. /// Represents a class used to load models from Byte Arrays using callbacks to map External Data and Textures.
  54. /// </summary>
  55. public class SimpleCustomAssetLoader
  56. {
  57. /// <summary>
  58. /// Loads a model from the given Byte Array data using the given callbacks to handle events/external data.
  59. /// </summary>
  60. /// <param name="data">The model data Byte Array.</param>
  61. /// <param name="modelExtension">The model file extension.</param>
  62. /// <param name="onError">The error event callback (optional).</param>
  63. /// <param name="onProgress">The loading progress event callback.</param>
  64. /// <param name="onModelFullyLoad">The model loading event callback.</param>
  65. /// <param name="customDataReceivingCallback">The event that returns a Stream to read the external data passed to it.</param>
  66. /// <param name="customFilenameReceivingCallback">The event that returns a file-system complete filename from the filename passed to it (optional).</param>
  67. /// <param name="customTextureReceivingCallback">The event that returns a Stream to read Texture data from the filename passed to it.</param>
  68. /// <param name="modelFilename">The model filename (optional).</param>
  69. /// <param name="wrapperGameObject">The GameObject to wrap the loaded model (optional).</param>
  70. /// <param name="assetLoaderOptions">The AssetLoaderOptions to use when loading the model (optional).</param>
  71. /// <param name="customData">Any custom data to pass to the loading method, which can be retrieved later (optional).</param>
  72. /// <returns>The AssetLoaderContext containing all common data regarding the model loading.</returns>
  73. public static AssetLoaderContext LoadModelFromByteData(
  74. byte[] data,
  75. string modelExtension,
  76. Action<IContextualizedError> onError,
  77. Action<AssetLoaderContext, float> onProgress,
  78. Action<AssetLoaderContext> onModelFullyLoad,
  79. Func<string, Stream> customDataReceivingCallback,
  80. Func<string, string> customFilenameReceivingCallback,
  81. Func<ITexture, Stream> customTextureReceivingCallback,
  82. string modelFilename = null,
  83. GameObject wrapperGameObject = null,
  84. AssetLoaderOptions assetLoaderOptions = null,
  85. object customData = null)
  86. {
  87. if (data == null || data.Length == 0)
  88. {
  89. throw new Exception("Missing model file byte data.");
  90. }
  91. return LoadModelFromStream(new MemoryStream(data), modelExtension, onError, onProgress, onModelFullyLoad, customDataReceivingCallback, customFilenameReceivingCallback, customTextureReceivingCallback, modelFilename, wrapperGameObject, assetLoaderOptions, customData);
  92. }
  93. /// <summary>
  94. /// Loads a model from the given Byte Array data using the given callbacks to handle events/external data.
  95. /// </summary>
  96. /// <param name="stream">The model data Stream.</param>
  97. /// <param name="modelExtension">The model file extension.</param>
  98. /// <param name="onError">The error event callback (optional).</param>
  99. /// <param name="onProgress">The loading progress event callback.</param>
  100. /// <param name="onModelFullyLoad">The model loading event callback.</param>
  101. /// <param name="customDataReceivingCallback">The event that returns a Stream to read the external data passed to it.</param>
  102. /// <param name="customFilenameReceivingCallback">The event that returns a file-system complete filename from the filename passed to it (optional).</param>
  103. /// <param name="customTextureReceivingCallback">The event that returns a Stream to read Texture data from the filename passed to it.</param>
  104. /// <param name="modelFilename">The model filename (optional).</param>
  105. /// <param name="wrapperGameObject">The GameObject to wrap the loaded model (optional).</param>
  106. /// <param name="assetLoaderOptions">The AssetLoaderOptions to use when loading the model (optional).</param>
  107. /// <param name="customData">Any custom data to pass to the loading method, which can be retrieved later (optional).</param>
  108. /// <returns>The AssetLoaderContext containing all common data regarding the model loading.</returns>
  109. public static AssetLoaderContext LoadModelFromStream(
  110. Stream stream,
  111. string modelExtension,
  112. Action<IContextualizedError> onError,
  113. Action<AssetLoaderContext, float> onProgress,
  114. Action<AssetLoaderContext> onModelFullyLoad,
  115. Func<string, Stream> customDataReceivingCallback,
  116. Func<string, string> customFilenameReceivingCallback,
  117. Func<ITexture, Stream> customTextureReceivingCallback,
  118. string modelFilename = null,
  119. GameObject wrapperGameObject = null,
  120. AssetLoaderOptions assetLoaderOptions = null,
  121. object customData = null)
  122. {
  123. if (stream == null)
  124. {
  125. throw new Exception("Missing model file byte data.");
  126. }
  127. if (string.IsNullOrWhiteSpace(modelExtension) && !string.IsNullOrWhiteSpace(modelFilename))
  128. {
  129. modelExtension = FileUtils.GetFileExtension(modelFilename);
  130. }
  131. if (string.IsNullOrWhiteSpace(modelExtension))
  132. {
  133. throw new Exception("Missing model extension parameter");
  134. }
  135. var simpleExternalDataMapper = ScriptableObject.CreateInstance<SimpleExternalDataMapper>();
  136. simpleExternalDataMapper.Setup(customDataReceivingCallback, customFilenameReceivingCallback);
  137. var simpleTextureMapper = ScriptableObject.CreateInstance<SimpleTextureMapper>();
  138. simpleTextureMapper.Setup(customTextureReceivingCallback);
  139. if (assetLoaderOptions == null)
  140. {
  141. assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  142. }
  143. assetLoaderOptions.ExternalDataMapper = simpleExternalDataMapper;
  144. assetLoaderOptions.TextureMappers = new TextureMapper[] { simpleTextureMapper};
  145. return AssetLoader.LoadModelFromStream(stream, modelFilename, modelExtension, null, onModelFullyLoad, onProgress, onError, wrapperGameObject, assetLoaderOptions, customData);
  146. }
  147. }
  148. }