TexturePackerProcessor.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System.IO;
  5. using Microsoft.Xna.Framework.Content.Pipeline;
  6. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  7. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  8. using MonoGame.Extended.Content.TexturePacker;
  9. namespace MonoGame.Extended.Content.Pipeline.TextureAtlases;
  10. [ContentProcessor(DisplayName = "TexturePacker Processor - MonoGame.Extended")]
  11. public class TexturePackerProcessor : ContentProcessor<ContentImporterResult<TexturePackerFileContent>, TexturePackerProcessorResult>
  12. {
  13. public override TexturePackerProcessorResult Process(ContentImporterResult<TexturePackerFileContent> input, ContentProcessorContext context)
  14. {
  15. if (input.Data.Meta.Image != null)
  16. {
  17. // Validates the texture exists and can be processed (fails build if missing)
  18. var externalRef = new ExternalReference<Texture2DContent>(input.Data.Meta.Image);
  19. context.BuildAndLoadAsset<Texture2DContent, Texture2DContent>(externalRef, nameof(TextureProcessor));
  20. }
  21. else if (input.Data.Meta.DataFormat == "monogame-extended")
  22. {
  23. foreach (var texture in input.Data.Textures)
  24. {
  25. string texturePath = Path.Combine(Path.GetDirectoryName(input.FilePath), texture.FileName);
  26. var externalRef = new ExternalReference<Texture2DContent>(texturePath);
  27. context.BuildAndLoadAsset<Texture2DContent, Texture2DContent>(externalRef, nameof(TextureProcessor));
  28. }
  29. }
  30. return new TexturePackerProcessorResult(input.Data);
  31. }
  32. }