Export and import
Texture packing
A game rarely loads every image of a character as a separate file. It loads a few large pages of an atlas, with every image placed on them, and a text file that says where each region is. Packing the images into an atlas is therefore part of almost every export. This article is about the Atlas section of the Export window, which writes only the atlas: choosing what to pack, the options of the packer, the several sizes of one atlas, the pack.json files and the preview that shows the layout before anything is written.
What to pack#
The Source switch at the top of the section has two choices. Skeleton images packs the images that the attachments of the skeleton use, and names the regions the way a runtime looks them up. Image folder packs every image of a folder that you choose with …, and names each region after its file. The second choice does not need a project at all: it is how you pack a folder of already cut-out parts, such as the frames of an effect. The same packer runs from the command line, which is covered in Command line.
The atlas is written as a .atlas text file, or as .atlas.txt when your Unity runtime looks for that name, which you choose in Atlas file. The pages are PNG or JPEG images that are named after the output: knight.png, then knight2.png for the second page and so on. The Texture atlas switch in the Data section of the Export window uses the same packer for the images of the exported skeleton.
Page size and layout#
Max width and Max height limit one page, 2048 by default. When the images do not fit, the packer adds pages. Min width and Min height set the smallest page that is allowed. Several switches shape the final size: Power of two, Divisible by 4 and Square round it up, and the options that your target hardware needs usually decide which you turn on.
Padding is the gap between two images and Edge padding is the gap between an image and the border of the page, both 2 pixels by default. Bleed stretches the edge pixels of each region into the padding by the given number of pixels, which stops dark or light seams from appearing at the edges of a region that is drawn scaled or rotated. Duplicate padding does the same across the entire free padding instead of a fixed number of pixels, and then Bleed has no effect.
Pack mode chooses the algorithm. Rectangles is the tightest general layout. Grid puts every image into an equal cell that fits the largest one, which suits the frames of a sequence that you want to read as a table. Polygons packs by the real opaque pixels of each image instead of its box, so irregular shapes can sit closer together. Grid and Polygons never rotate images. With Rectangles, Allow rotation may turn an image by 90 degrees to make it fit, and the runtime must support that.
Trimming and pixels#
Strip whitespace trims the transparent border of each image and stores the trimmed size and the offset, so the image is drawn in the same place. It is on by default and saves a lot of space. Ignore blank drops an image that is entirely transparent. Premultiply alpha writes pages with the color already multiplied by the alpha, which some runtimes and blend modes expect, and the runtime must be set to match.
Filter is the texture filter written into the .atlas page. Linear smooths regions that are scaled or rotated, and Nearest keeps pixel art crisp. Wrap writes a repeat hint for a page with a tiling texture, and it is only a hint for the runtime. JPEG pages have a JPEG quality setting and no alpha, so use them only when the images have none.
Several sizes#
Scales writes extra versions of the atlas at other sizes, which is the answer to games that ship one set of textures for high and another for low resolution displays. Type factor and suffix pairs separated by commas, for example 0.5:@0.5x, 2:@2x. For each pair Ligamen writes another set of pages and another .atlas file next to the first one, such as knight@0.5x.png and knight@0.5x.atlas. Resample decides how those pages are resized: Smooth blends pixels, Nearest keeps the hard edges of pixel art. Auto scale shrinks the output in small steps until every group of pages fits on one page.
Image folders and pack.json#
For an image folder, three options control the names of the regions. Combine subdirectories packs the images of subfolders onto the same pages as the folder itself, Flatten paths removes the subfolders from the region names, which then must be unique, and Indexes turns a trailing _number of a file name into the index of a region, which is how sequences are named.
A pack.json file in an image folder overrides these settings for that folder and its subfolders. It uses the keys of Spine's texture packer, such as maxWidth, paddingX, rotation, pot, square, wrapX, combineSubdirectories, flattenPaths, useIndexes and ignore. Settings of the whole atlas, such as premultiplyAlpha and outputFormat, are read only from the one in the root folder. A subfolder that has its own pack.json gets its own pages even when Combine subdirectories is on, and an unknown key produces a warning, not an error. In the Data section, the Image folders choice of Pack follows these files, and Attachments ignores them.
The preview#
While you change options, the window shows the layout that the export will write: boxes for the pages with every region in them, one line such as 3 pages · 91% fill · ~48 MB in memory, and one line for each page with the name it will get and its size and fill. The memory is that of the pages as uncompressed RGBA, which is the cost that matters in a game. The preview is calculated in the background, so the window stays responsive with a large folder. Use it to try a smaller Max width or a different Pack mode and see at once what it does to the number of pages, before you export.