Version: 2021.1
LanguageEnglish
  • C#

WebCamTexture.GetPixels32

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

Declaration

public Color32[] GetPixels32(Color32[] colors = null);

Parameters

colors Optional array to receive pixel data.

Returns

Color32[] An array that contains a copy of the requested pixel colors, represented by Color32 structs.

Description

Retrieves a copy of the the pixel color data. The colors are represented by Color32 structs.

A single call to this function is usually faster than multiple calls to GetPixel, especially for large textures. This function is faster than GetPixels and uses less memory because it does not perform integer-to-float conversions

The returned array is a flattened 2D array, where the data appears row by row: the pixels are laid out left to right, bottom to top. The dimensions of the array are width * height of the texture.

You can optionally pass in an array of Color32 structs to avoid allocating new memory each frame. This can improve performance if you are continuously reading data from the camera. The array must be initialized to the dimensions width * height of the texture. If you don't pass an array, Unity will allocate one and return it.

using UnityEngine;

public class Example : MonoBehaviour { WebCamTexture webcamTexture; Color32[] data;

void Start() { // Start web cam feed webcamTexture = new WebCamTexture(); webcamTexture.Play(); data = new Color32[webcamTexture.width * webcamTexture.height]; }

void Update() { webcamTexture.GetPixels32(data); // Do processing of data here. } }