Parameter | Description |
---|---|
src | The source texture to copy from, specified as a Texture. |
dst | The destination texture to copy to, specified as a Texture. |
srcElement | The element in the source texture to copy from. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to 0 if src is a 2D texture. |
srcMip | The mipmap level to copy from. The range is 0 through the texture's Texture.mipmapCount. The default value is 0 . |
dstElement | The element in the destination texture to copy to. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to 0 if `dst` is a 2D texture. |
dstMip | The mipmap level to write to. The range is 0 through the texture's Texture.mipmapCount. The default value is 0 . |
srcX | The starting x coordinate of src to copy a region from. Accepted values range from 0 to the width of src at the specified srcMip . 0 is the left of the texture. |
srcY | The starting y coordinate of src to copy a region from. Accepted values range from 0 to the height of src at the specified srcMip . 0 is the bottom of the texture. |
srcWidth | The width of the region to copy. Accepted values are greater than or equal to 0 and ensure that the region fits within src at the specified srcMip considering offset (srcX, srcY) , and within dst at the specified dstMip considering offset (dstX, dstY) . |
srcHeight | The height of the region to copy. Accepted values are greater than or equal to 0 and ensure that the region fits within src at the specified srcMip considering offset (srcX, srcY) , and within dst at the specified dstMip considering offset (dstX, dstY) . |
dstX | The starting x coordinate of dst to copy a region to. Accepted values range from 0 to the width of dst at the specified dstMip . 0 is the left of the texture. |
dstY | The starting y coordinate of dst to copy a region to. Accepted values range from 0 to the height of dst at the specified dstMip . 0 is the bottom of the texture. |
Copies pixel data from one texture to another.
This method copies pixel data from one texture to another on the GPU. If Texture.isReadable is true
for both src
and dst
, the method also copies pixel data on the CPU. If Texture.isReadable is false
for either src
or dst
, CopyTexture
becomes one of the fastest ways to copy a texture. Warning: Be careful when using an Apply
method such as Texture2D.Apply after CopyTexture
, because you might copy old or undefined CPU texture data to the GPU.
To use CopyTexture
, the following must be the same in both the source and destination texture areas:
You might be able to copy between incompatible formats depending on your graphics API. For example, on some APIs you can copy between formats with the same bit width.
Depending on your graphics API, you might not be able to copy between different types of textures. For more information on compatibility, see SystemInfo.copyTextureSupport and CopyTextureSupport.
If src
is a depth-only render texture, you must copy the whole texture, not part of it. A depth-only render texture has its color buffer set to a color format of None
and its depth buffer set to a valid RenderTexture.depthStencilFormat.
When you use Texture2D.ignoreMipmapLimit, textures can have a variety of mipmap limit settings. This means you may not be able to copy from one mipmap level to another, because for example the source texture is limited to half resolution and the destination texture is limited to quarter resolution.
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:
The following sample shows how you can copy to or from a Texture2DArray, when Texture2Ds are subject to a master texture limit different from 0:
using UnityEngine;
public class CopyTextureSample : MonoBehaviour { // Sample to copy all available mips: inTex -> outArray (no master texture limits) -> outTex [SerializeField] Texture2D inTex; [SerializeField] Texture2DArray outArray; [SerializeField] Texture2D outTex; [SerializeField] bool considerMasterTextureLimits;
void Start() { int width = inTex.width; int height = inTex.width; outArray = new Texture2DArray(width, height, 1, inTex.format, true); outTex = new Texture2D(width, height, inTex.format, true);
if (!considerMasterTextureLimits) { // Texture2D -> Texture2DArray // Master Texture Limit: "1: Half Resolution" => copies into mips that are now too large; will copy each mip of inTex into a quarter of outArray for (int mip = 0; mip < inTex.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(inTex, 0, mip, 0, 0, copyWidth, copyHeight, outArray, 0, mip, 0, 0); }
// Texture2DArray -> Texture2D // Master Texture Limit: "1: Half Resolution" => errors, since we try to copy into mips that are now too small for (int mip = 0; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(outArray, 0, mip, 0, 0, copyWidth, copyHeight, outTex, 0, mip, 0, 0); } } else // considering master texture limits { int masterTextureLimit = QualitySettings.masterTextureLimit; // Texture2D -> Texture2DArray // Master Texture Limit: "1: Half Resolution" => mip0 of outArray is not written to, other mips copy as expected // (ALTERNATIVE: if outArray creation already considered masterTextureLimit for its dimensions, // the CopyTexture call can ignore masterTextureLimit since the mips will line up again) for (int mip = 0; mip < inTex.mipmapCount - masterTextureLimit; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip + masterTextureLimit; Graphics.CopyTexture(inTex, 0, srcMip, 0, 0, copyWidth, copyHeight, outArray, 0, dstMip, 0, 0); }
// Texture2DArray -> Texture2D // Master Texture Limit: "1: Half Resolution" => mip0 of outArray is not copied (but outTex does not upload it to GPU anyway) for (int mip = masterTextureLimit; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip - masterTextureLimit; Graphics.CopyTexture(outArray, 0, srcMip, 0, 0, copyWidth, copyHeight, outTex, 0, dstMip, 0, 0); } } } }
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.
Compressed texture formats add some restrictions to CopyTexture
with a region variant. For example, PVRTC formats
are not supported since they are not block-based (for these formats you can only copy whole texture or whole mip level).
For block-based formats (e.g. DXT, BCn, ETC), the region size and coordinates must be a multiple of compression block size
(4 pixels for DXT).
Even if Texture.isReadable is true
, CopyTexture
doesn't copy pixel data on the CPU if you copy only a region of a compressed texture.
Additional resources: CopyTextureSupport.