# GetRawTextureData

> Gets the raw data from a texture, as a copy.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.5/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

## GetRawTextureData()

Gets the raw data from a texture, as a copy.

```csharp
public byte[] GetRawTextureData()
```

### Returns

| Type                                                           | Description                                  |
| -------------------------------------------------------------- | -------------------------------------------- |
| [byte\[\]](https://learn.microsoft.com/dotnet/api/system.byte) | A byte array that contains raw texture data. |

### Remarks

This version of the `GetRawTextureData` method returns a copy of the raw texture data on the CPU. [Texture.isReadable](/engine/6000.5/script-reference/unityengine/texture/isreadable.md) must be `true`.

If you don't need a copy or if you want to modify the data directly, use the version of this function that returns a `NativeArray`, or [Texture2D.GetPixelData](/engine/6000.5/script-reference/unityengine/texture2d/getpixeldata.md).

You can use the returned array with [Texture2D.LoadRawTextureData](/engine/6000.5/script-reference/unityengine/texture2d/loadrawtexturedata.md). 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](/engine/6000.5/script-reference/unityengine/graphics/copytexture.md) 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 `NativeArray`, and use [Color32](/engine/6000.5/script-reference/unityengine/color32.md) for `T` or a struct that matches the format of the texture.

`GetRawTextureData` throws an exception when it fails.

### Examples

```csharp
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.

```csharp
public NativeArray<T> GetRawTextureData<T>() where T : struct
```

### Returns

| Type                                                                                 | Description                                                                     |
| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------- |
| [NativeArray\<T>](/engine/6000.5/script-reference/unity/collections/nativearray1.md) | A native array that points directly to the texture's data buffer in CPU memory. |

### Remarks

This version of the `GetRawTextureData` method returns a [NativeArray\<T>](/engine/6000.5/script-reference/unity/collections/nativearray1.md) that points directly to the texture's data on the CPU. The array doesn't contain a copy of the data, so `GetRawTextureData` doesn't allocate any memory. To return a copy, use the version that returns a `byte[]` array instead.

You can also use [Texture2D.GetPixelData](/engine/6000.5/script-reference/unityengine/texture2d/getpixeldata.md). You can use `GetPixelData` to get a mipmap level instead of the entire texture.

You usually use a struct for `T` that matches the structure of a pixel in the texture, for example [Color32](/engine/6000.5/script-reference/unityengine/color32.md) if the texture format uses RGBA pixels in 32-bit format, such as [TextureFormat.RGBA32](/engine/6000.5/script-reference/unityengine/textureformat/rgba32.md).

The returned array contains the entire texture according to its width, height, data [Texture2D.format](/engine/6000.5/script-reference/unityengine/texture2d/format.md) and [mipmapCount](/engine/6000.5/script-reference/unityengine/texture/mipmapcount.md). 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](/engine/6000.5/script-reference/unityengine/color32.md) for `T` (4 bytes per pixel). You can use the experimental [GraphicsFormatUtility.ComputeMipmapSize](/engine/6000.5/script-reference/unityengine/experimental/rendering/graphicsformatutility/computemipmapsize.md) API to calculate the size of a mipmap level.

The 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](/engine/6000.5/script-reference/unityengine/texture2d/apply.md) 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 `T` such as `byte`, `GetRawTextureData` may fail because the `NativeArray` would exceed the maximum length (`Int32.MaxValue`). To avoid this, use a larger type or struct.

`GetRawTextureData` throws an exception when it fails.

Additional Resources: [Texture2D.Apply](/engine/6000.5/script-reference/unityengine/texture2d/apply.md), [Texture2D.SetPixels](/engine/6000.5/script-reference/unityengine/texture2d/setpixels.md), [Texture2D.SetPixels32](/engine/6000.5/script-reference/unityengine/texture2d/setpixels32.md), [Texture2D.LoadRawTextureData](/engine/6000.5/script-reference/unityengine/texture2d/loadrawtexturedata.md), [Texture2D.GetPixelData](/engine/6000.5/script-reference/unityengine/texture2d/getpixeldata.md).

### Examples

```csharp
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();
    }
}
```
