Version: 2020.3

Texture2D.PackTextures

切换到手册
public Rect[] PackTextures (Texture2D[] textures, int padding, int maximumAtlasSize, bool makeNoLongerReadable);

参数

textures 要打包到图集中的纹理的数组。
padding 打包的纹理之间的像素填充。
maximumAtlasSize 生成的纹理的最大大小。
makeNoLongerReadable 是否应将纹理标记为不再可读?

返回

Rect[] 包含每个输入纹理在图集中的 UV 坐标的矩形数组,如果打包失败,则为 null。

描述

将多个纹理打包到一个纹理图集中。

该函数将使用由提供的纹理制作的图集替换当前纹理。 打包后,任何纹理的大小、格式和多级渐进纹理都可能发生更改。

生成的纹理图集的大小将满足所有输入纹理的需求, 但每个维度最多只能达到 /maximumAtlasSize/。如果无法将输入纹理全部装入所需大小的纹理图集, 则将输入纹理缩小后装入。

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.

如果 makeNoLongerReadablemakeNoLongerReadable,则纹理将被标记为不再可读取, 并且在上传到 GPU 后内存将被释放。 默认情况下,makeNoLongerReadable 被设置为 /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); } }