ExternalDataMapperSample.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.IO;
  3. using TriLibCore.Mappers;
  4. using TriLibCore.Utils;
  5. using UnityEngine;
  6. namespace TriLibCore.Samples
  7. {
  8. /// <summary>
  9. /// Represents a class that finds external resources at the given model base path.
  10. /// </summary>
  11. public class ExternalDataMapperSample : ExternalDataMapper
  12. {
  13. /// <summary>
  14. /// Tries to find the given external data source using the original resource filename and the context parameters.
  15. /// </summary>
  16. /// <param name="assetLoaderContext">The Asset Loader Context reference. Asset Loader Context contains the Model loading data.</param>
  17. /// <param name="originalFilename">The source data original filename.</param>
  18. /// <param name="finalPath">The found data final Path.</param>
  19. /// <returns>The external data source Stream, if found. Otherwise <c>null</c>.</returns>
  20. public override Stream Map(AssetLoaderContext assetLoaderContext, string originalFilename, out string finalPath)
  21. {
  22. finalPath = $"{assetLoaderContext.BasePath}/{FileUtils.GetFilename(originalFilename)}";
  23. if (File.Exists(finalPath))
  24. {
  25. Debug.Log($"Found external file at: {finalPath}");
  26. return File.OpenRead(finalPath);
  27. }
  28. throw new Exception($"File {originalFilename} not found.");
  29. }
  30. }
  31. }