GetRawTextureData
Gets the raw data from a texture, as a copy.
Read time 4 minutesLast updated 10 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
GetRawTextureData()
Gets the raw data from a texture, as a copy.
public byte[] GetRawTextureData()
Returns
Type | Description |
|---|---|
| byte[] | A byte array that contains raw texture data. |
Remarks
This version of the method returns a copy of the raw texture data on the CPU. Texture.isReadable must be .
GetRawTextureDatatrueIf you don't need a copy or if you want to modify the data directly, use the version of this function that returns a , or Texture2D.GetPixelData.
NativeArrayYou can use the returned array with Texture2D.LoadRawTextureData. This allows you to serialize and load a textures of any format, including compressed textures, and load the data into a texture later.
The CPU texture might not match the GPU texture if you used a method such as Graphics.CopyTexture that only updates GPU textures, so the CPU texture is out of sync.
The maximum size of the returned array is 2 gigabytes, because C# arrays have a maximum of 2 billion elements. If you need more than 2 gigabytes of data, use the version of this function that returns a , and use Color32 for or a struct that matches the format of the texture.
NativeArrayTGetRawTextureDataExamples
using UnityEngine;class CopyTexture : MonoBehaviour{ // the source texture. Texture2D tex; void Start() { // Create a copy of the texture by reading and applying the raw texture data. Texture2D texCopy = new Texture2D(tex.width, tex.height, tex.format, tex.mipmapCount > 1); texCopy.LoadRawTextureData(tex.GetRawTextureData()); texCopy.Apply(); }}
GetRawTextureData<T>()
Gets the raw data from a texture, as an array that points to memory.
public NativeArray<T> GetRawTextureData<T>() where T : struct
Returns
Type | Description |
|---|---|
| NativeArray<T> | A native array that points directly to the texture's data buffer in CPU memory. |
Remarks
This version of the method returns a NativeArray<T> that points directly to the texture's data on the CPU. The array doesn't contain a copy of the data, so doesn't allocate any memory. To return a copy, use the version that returns a array instead.
GetRawTextureDataGetRawTextureDatabyte[]You can also use Texture2D.GetPixelData. You can use to get a mipmap level instead of the entire texture.
GetPixelDataYou usually use a struct for that matches the structure of a pixel in the texture, for example Color32 if the texture format uses RGBA pixels in 32-bit format, such as TextureFormat.RGBA32.
TThe returned array contains the entire texture according to its width, height, data Texture2D.format and mipmapCount. For example, if the texture is 16 × 8 pixels and RGBA32 format with no mipmaps, the method returns an array with a size of 512 bytes (16 × 8 × 4 bytes), and contains 128 elements if you use Color32 for (4 bytes per pixel). You can use the experimental GraphicsFormatUtility.ComputeMipmapSize API to calculate the size of a mipmap level.
TThe array starts with mipmap level 0.
You can read from and write to the returned array to get and change the data directly in CPU memory. If you write to the array, you must then call the Texture2D.Apply method to upload the texture to the GPU.
Use the returned array immediately. If you store the array and use it later, it might not point to the correct memory location if the texture has been modified or updated.
If you use a small type for such as , may fail because the would exceed the maximum length (). To avoid this, use a larger type or struct.
TbyteGetRawTextureDataNativeArrayInt32.MaxValueGetRawTextureDataAdditional Resources: Texture2D.Apply, Texture2D.SetPixels, Texture2D.SetPixels32, Texture2D.LoadRawTextureData, Texture2D.GetPixelData.
Examples
using UnityEngine;public class ExampleScript : MonoBehaviour{ void Start() { var texture = new Texture2D(128, 128, TextureFormat.RGBA32, false); GetComponent<Renderer>().material.mainTexture = texture; // RGBA32 texture format data layout exactly matches Color32 struct var data = texture.GetRawTextureData<Color32>(); // fill texture data with a simple pattern Color32 orange = new Color32(255, 165, 0, 255); Color32 teal = new Color32(0, 128, 128, 255); int index = 0; for (int y = 0; y < texture.height; y++) { for (int x = 0; x < texture.width; x++) { data[index++] = ((x & y) == 0 ? orange : teal); } } // upload to the GPU texture.Apply(); }}