Version: 2021.2
LanguageEnglish
  • C#

Texture2DArray.GetPixelData

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Declaration

public NativeArray<T> GetPixelData(int mipLevel, int element);

Parameters

mipLevel The mip level to reference.
element The array slice to reference.

Returns

NativeArray<T> The view into the texture system memory data buffer.

Description

Gets raw data from a Texture for reading or writing.

This function returns a direct "view" into the Texture pixel data as a Unity.Collections.NativeArray.

A slice of the data will be returned according to the requested mip level and array element. For example, for a 16x16 sized Texture of RGBA32 format, getting the mip=1 level (8x8 size) will result in a 256-byte array or a 64-element array if Color32 is used as a type (this also applies to any array element).

You can read from and write to the returned array. If you write to it, you must call the Apply method to upload the Texture to the GPU. If an array returned by GetPixelData was used to fill up a non-0 level mipmap, then updateMipmaps must be set to false before calling the Apply method.

GetPixelData does not allocate memory; the returned NativeArray directly points to the texture system memory data buffer.

It is recommended to immediately use or modify the data retrieved by this method and to not store the returned array for later use as the returned array can become invalid (i.e. it no longer points to valid memory) if the Texture is modified or updated after you called this method.

GetPixelData throws an exception when it fails.

See Also: Apply, SetPixels, SetPixels32, GetPixelData.

using UnityEngine;

public class ExampleScript : MonoBehaviour { public void Start() { var m_Texture2DArray = new Texture2DArray(16, 16, 3, TextureFormat.RGBA32, true); var mip0Element1 = m_Texture2DArray.GetPixelData<Color32>(1, 1);

// pixels in mip = 1 from the second array element are filled with white color for (int i = 0; i < mip0Element1.Length; i++) { mip0Element1[i] = new Color32(255, 255, 255, 255); }

m_Texture2DArray.Apply(false); } }