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.
If you have compressed all input textures, and the following conditions are met, Unity also compresses the output atlas:
• All input textures are of the same compressed type, or are a mixture of compatible types
• padding
is zero
or
• All input textures do not have mipmaps
If any of the input images have mipmaps and the padding value is greater than zero, the atlas texture is uncompressed. This is because of alignment restrictions on the compressed data.
If you have not compressed one or more input textures, the atlas remains in the uncompressed format RGBA32.
If the texture atlas remains compressed and has a padding value is greater than zero, the padding value is rounded up to the next multiple of four. This happens because of alignment restrictions on compressed data.
This is a shortlist of compressed formats. You can find the full list in TextureFormat:
• ETC
• ETC2
• EAC
• DXT
• ASTC
• PVRTC
You can mix these types in an atlas, where they stay compressed:
• DXT1 and TextureFormat::pref:DXT5 result in a DXT5 atlas.
• ETC2_RGB, ETC_RGB4, and ETC2_RGBA8 restult in a ETC2_RGBA8 atlas.
If none of the input textures have mipmaps, the atlas also has no mipmaps.
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); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.