Parameter | Description |
---|---|
textures | Array of textures to pack into the atlas. |
padding | Padding in pixels between the packed textures. |
maximumAtlasSize | Maximum size of the resulting texture. |
makeNoLongerReadable | Should the texture be marked as no longer readable? |
Rect[] An array of rectangles containing the UV coordinates in the atlas for each input texture, or null if packing fails.
Packs multiple Textures into a texture atlas.
This function will replace the current texture with the atlas made from the supplied textures.
The size, format and mipmaps of any of the textures can change after packing.
The resulting texture atlas will be as large as needed to fit all input textures but only up to maximumAtlasSize
in
each dimension. If the input textures can't all fit into a texture atlas of the desired size then they will be
scaled down to fit.
The following conditions will cause the atlas to be created as an uncompressed texture using the TextureFormat.RGBA32 format:
If the texture atlas is compressed and has a padding value greater than zero, the padding value is rounded up to the next multiple of four. This happens because of alignment restrictions on compressed texuture formats.
The atlas packing code only supports the following input texture formats:
All input textures must have the same compression format or be a mixture of compatible compression formats, otherwise the atlas format falls back to uncompressed. Variants of the same compressed format can be mixed in an atlas, where they stay compressed:
However, if the input textures mix unrelated formats, such as BC7 with ASTC 4x4 or DXT5, this causes the atlas packing code to fall back to TextureFormat.RGBA32.
If none of the input textures have mipmaps, the atlas also has no mipmaps.
Using compressed input textures of varying sizes with mipmaps will limit the resulting atlas mipcount to that of the smallest input texture minus two.
This is to ensure each atlas item mip level fills at least one block of compressed data. This can lead to reduced GPU performance as the larger atlas items will be limited in their ability to reduce texture cache pressure by sampling lower resolution mip levels. For optimal performance it is recommended to pack input textures of similar sizes into different atlases.
Take the following compressed atlas example:
The resulting compressed atlas will only have 7 mip levels:
Packing both textures into different atlases with similar item sizes, such as one for items with a minimum dimension of 256 and another with a minimum of 1024, will reduce the number of mip levels that are dropped when atlassing.
If makeNoLongerReadable
is true
then the texture will be marked as no longer readable
and memory will be freed after uploading to the GPU.
By default makeNoLongerReadable
is set to false
.
using UnityEngine;
public class Example : MonoBehaviour { // Source textures. Texture2D[] atlasTextures;
// Rectangles for individual atlas textures. Rect[] rects;
void Start() { // Pack the individual textures into the smallest possible space, // while leaving a two pixel gap between their edges. Texture2D atlas = new Texture2D(8192, 8192); rects = atlas.PackTextures(atlasTextures, 2, 8192); } }