TextureMapperSample.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.IO;
  3. using TriLibCore.Interfaces;
  4. using TriLibCore.Mappers;
  5. using TriLibCore.Utils;
  6. using UnityEngine;
  7. namespace TriLibCore.Samples
  8. {
  9. /// <summary>
  10. /// Represents a class that finds textures at the given model base path.
  11. /// </summary>
  12. public class TextureMapperSample : TextureMapper
  13. {
  14. /// <summary>Tries to retrieve a Stream to the Texture native data based on the given context.</summary>
  15. /// <param name="assetLoaderContext">The Asset Loader Context reference. Asset Loader Context contains the information used during the Model loading process, which is available to almost every Model processing method</param>
  16. /// <param name="texture">The source Texture to load the Stream from.</param>
  17. /// <returns>Return the context containing the texture data.</returns>
  18. public override TextureLoadingContext Map(AssetLoaderContext assetLoaderContext, ITexture texture)
  19. {
  20. var finalPath = $"{assetLoaderContext.BasePath}/{FileUtils.GetFilename(texture.Filename)}";
  21. if (File.Exists(finalPath))
  22. {
  23. var textureLoadingContext = new TextureLoadingContext
  24. {
  25. Context = assetLoaderContext,
  26. Stream = File.OpenRead(finalPath),
  27. Texture = texture
  28. };
  29. Debug.Log($"Found texture at: {finalPath}");
  30. return textureLoadingContext;
  31. }
  32. throw new Exception($"Texture {texture.Filename} not found.");
  33. }
  34. }
  35. }