packSprites() public static method

Packs a list of sprites into a single big texture, recording where each one was stored.
public static packSprites ( IList sourceSprites, ICollection outputSprites, bool isCompressed, ContentProcessorContext context ) : PixelBitmapContent
sourceSprites IList
outputSprites ICollection
isCompressed bool
context ContentProcessorContext
return PixelBitmapContent
        /// <summary>
        /// Converts an array of sprite filenames into a texture atlas object.
        /// </summary>
        public override TextureAtlasContent Process(string[] input, ContentProcessorContext context)
        {
            logger = context.Logger;
            var textureAtlas = new TextureAtlasContent
            {
                animationFPS = (int)animationFPS
            };
            var sourceSprites = new List <BitmapContent>();
            var imagePaths    = new List <string>();

            // first, we need to sort through and figure out which passed in paths are images and which are folders
            foreach (var inputPath in input)
            {
                // first, the easy one. if it isnt a directory its an image so just add it
                if (!Directory.Exists(inputPath))
                {
                    if (isValidImageFile(inputPath))
                    {
                        imagePaths.Add(inputPath);
                    }
                    continue;
                }

                // we have a directory. we need to recursively add all images in all subfolders
                processDirectory(inputPath, imagePaths, textureAtlas);
            }

            // Loop over each input sprite filename
            foreach (var inputFilename in imagePaths)
            {
                // Store the name of this sprite.
                var spriteName = getSpriteNameFromFilename(inputFilename, input);
                textureAtlas.spriteNames.Add(spriteName, sourceSprites.Count);
                context.Logger.LogMessage("Adding texture: {0}", spriteName);

                // Load the sprite texture into memory.
                var textureReference = new ExternalReference <TextureContent>(inputFilename);
                var texture          = context.BuildAndLoadAsset <TextureContent, TextureContent>(textureReference, "TextureProcessor");

                if (inputFilename.Contains(".9"))
                {
                    logger.LogMessage("\tprocessing nine patch texture");
                    textureAtlas.nineSliceSplits[spriteName] = processNinePatchTexture(texture);
                }

                sourceSprites.Add(texture.Faces[0][0]);
            }

            // Pack all the sprites into a single large texture.
            var packedSprites = TextureAtlasPacker.packSprites(sourceSprites, textureAtlas.spriteRectangles, compressTexture, context);

            textureAtlas.texture.Mipmaps.Add(packedSprites);

            if (compressTexture)
            {
                textureAtlas.texture.ConvertBitmapType(typeof(Dxt5BitmapContent));
            }

            return(textureAtlas);
        }
Example #2
0
        /// <summary>
        /// Converts an array of sprite filenames into a texture atlas object.
        /// </summary>
        public override TextureAtlasContent Process(string[] input, ContentProcessorContext context)
        {
            logger = context.Logger;
            var textureAtlas = new TextureAtlasContent
            {
                animationFPS = (int)animationFPS,
            };
            var sourceSprites = new List <BitmapContent>();
            var imagePaths    = new List <string>();

            // first, we need to sort through and figure out which passed in paths are images and which are folders
            foreach (var inputPath in input)
            {
                // first, the easy one. if it isnt a directory its an image so just add it
                if (!Directory.Exists(inputPath))
                {
                    if (isValidImageFile(inputPath))
                    {
                        imagePaths.Add(inputPath);
                    }
                    continue;
                }

                // we have a directory. we need to recursively add all images in all subfolders
                processDirectory(inputPath, imagePaths, textureAtlas);
            }

            // Loop over each input sprite filename
            foreach (var inputFilename in imagePaths)
            {
                // Store the name of this sprite.
                var spriteName = getSpriteNameFromFilename(inputFilename, input);
                textureAtlas.spriteNames.Add(spriteName, sourceSprites.Count);
                context.Logger.LogMessage("Adding texture: {0}", spriteName);

                // Load the sprite texture into memory.
                var textureReference = new ExternalReference <TextureContent>(inputFilename);
                var texture          = context.BuildAndLoadAsset <TextureContent, TextureContent>(textureReference, "TextureProcessor");

                if (inputFilename.Contains(".9"))
                {
                    logger.LogMessage("\tprocessing nine patch texture");
                    textureAtlas.nineSliceSplits[spriteName] = processNinePatchTexture(texture);
                }

                // Convert sprite's color key color to transparent
                if (colorKeyEnabled)
                {
                    var originalType = texture.Faces[0][0].GetType();
                    try
                    {
                        texture.ConvertBitmapType(typeof(PixelBitmapContent <Vector4>));
                    }
                    catch (Exception ex)
                    {
                        context.Logger.LogImportantMessage("Could not convert input texture for processing. " + ex.ToString());
                        throw ex;
                    }
                    var bmp = (PixelBitmapContent <Vector4>)texture.Faces[0][0];
                    bmp.ReplaceColor(colorKeyColor.ToVector4(), Vector4.Zero);
                    texture.Faces[0][0] = bmp;
                    texture.ConvertBitmapType(originalType);
                }

                sourceSprites.Add(texture.Faces[0][0]);
            }

            // Pack all the sprites into a single large texture.
            var packedSprites = TextureAtlasPacker.packSprites(sourceSprites, textureAtlas.spriteRectangles, compressTexture, context);

            textureAtlas.texture.Mipmaps.Add(packedSprites);

            if (compressTexture)
            {
                textureAtlas.texture.ConvertBitmapType(typeof(Dxt5BitmapContent));
            }

            return(textureAtlas);
        }