Version: 2020.3
public static void CopyTexture (Texture src, Texture dst);
public static void CopyTexture (Texture src, int srcElement, int srcMip, Texture dst, int dstElement, int dstMip);
public static void CopyTexture (Texture src, int srcElement, int srcMip, int srcX, int srcY, int srcWidth, int srcHeight, Texture dst, int dstElement, int dstMip, int dstX, int dstY);

参数

src 源纹理。
dst 目标纹理。
srcElement 源纹理元素(立方体贴图面、纹理数组层或 3D 纹理深度切片)。
srcMip 源纹理的 Mipmap 级别。
dstElement 目标纹理元素(立方体贴图面、纹理阵列层或 3D 纹理深度切片)。
dstMip 目标纹理的 Mipmap 级别。
srcX 要复制的源纹理区域(左侧为零)的 X 坐标。
srcY 要复制的源纹理区域(底部为零)的 Y 坐标。
srcWidth 要复制的源纹理区域的宽度。
srcHeight 要复制的源纹理区域的高度。
dstX 目标纹理中的复制目标区域(左侧为零)的 X 坐标。
dstY 目标纹理中的复制目标区域(底部为零)的 Y 坐标。

描述

复制纹理内容。

该函数可在纹理之间高效复制像素数据。 此外,它还可以从元素(例如立方体贴图面)或特定的 Mip 级别以及纹理的子区域进行复制。

When you copy from an element it does not perform any scaling. As a result, the source and destination sizes must be the same. Texture formats should be compatible, for example, TextureFormat.ARGB32 and RenderTextureFormat.ARGB32. MSAA sample counts for render textures should also be the same. Generally, you can always copy formats that are exactly the same, but format compatibility does vary between graphics APIs. On some platforms (e.g. D3D11) you can also copy between formats that are of the same bit width.

You can load Textures at different resolutions by using QualitySettings.masterTextureLimit. Note that this affects CopyTextures as you cannot copy a full mip between textures with different master texture limit values. If you need to copy between textures with a different master texture limit, use the region-based overload. The region-based overload adjusts the source rectangle based on the source texture's master texture limit. It also adjusts the destination offset based on the destination's master texture limit. For example, copying a 128x128 area from position 16,16 to position 32,32 results in the following behaviours in these example cases: When the master texture limit is set to 0: Unity performs all copies as expected. When the master texture limit is set to 2 and both textures are subject to the master texture limit: Unity adjusts the source rectangle to 32x32 and adjusts the offset to 4,4. Unity changes the destination offset to 8x8. Unity performs all copies as expected without awareness of the master texture limit setting. When the master texture limit is set to 2, the source is a regular texture, and the destination is an array texture (array textures are never subject to the master texture limit): Unity adjusts the source rectangle to 32x32 and adjusts the offset to 4,4. Unity doesn't change the destination offset, which remains at 32x32. Mipmap level arguments always apply to the texture as loaded under the current master texture limit. For example, a 256x256 texture with master texture limit set to 0 mip 1 refers to a 128x128 mip. However, if the mastertexture limit is set to 2 on that texture, mip 1 refers to a 32x32 mip. This means that in many cases when using CopyTexture you do not need to take the master texture limit into account in your calls. In less common calls (for example, copying from Texture2D to TextureArray) you do need to adjust for it. To copy textures in a cubemap array, calculate the destination element as 6 * cubemapIndex + faceIndex. As a result, the six faces from the cubemap at index 0 are elements 0,1,2... 5. The six faces from the cubemap at array index 1 are 6,7 .... 11 and so on.

压缩纹理格式通过区域变体对 CopyTexture 添加了一些限制。例如,不支持 PVRTC 格式, 因为该格式不基于方块(对于这些格式,您只能复制整个纹理或整个 Mip 级别)。 对于基于方块的格式(例如,DXT、ETC),区域大小和坐标必须是压缩方块大小的倍数 (对于 DXT 为 4 倍像素)。

如果源和目标纹理都标记为“可读”(即系统内存中存在 用于 CPU 读取/写入的数据副本),这些函数也将复制它。

有些平台可能没有全部纹理复制的功能(例如,从渲染纹理 复制到常规纹理中)。请参阅 CopyTextureSupport 并使用 SystemInfo.copyTextureSupport 进行检查。

CopyTexture 之后调用 Texture2D.ApplyTexture2DArray.ApplyTexture3D.Apply 会产生未定义的结果,因为 CopyTexture 以独占方式对 GPU 端数据进行操作,而 Apply 会将数据从 CPU 端传输到 GPU 端。

另请参阅:CopyTextureSupport