Search code examples
unity-game-enginegetpixel

Using GetPixels correctly in Unity


I'm trying to learn how to use the GetPixels function and I'm trying something simple for now - to get the pixels of the current object of a 2 x 2 area. I just want to display the results in the console first so I can better understand the results of GetPixels.

public class timecode : MonoBehaviour {

Color[] pixColors = GetPixels(0, 0, 2, 2);

// Use this for initialization
void Start () {

    Debug.Log(pixColors [0]);
}


// Update is called once per frame
void Update () {
}
}

I'm getting "The name `GetPixels' does not exist in the current context"


Solution

  • You need a Texture2D to access the GetPixels Method, try to add a public Texture2D Variable, assign a texture to it in the unity inspector, and then Get the Pixels with yourTextureVariable.GetPixels(...).

    Take a look at the documentation: https://docs.unity3d.com/ScriptReference/Texture2D.GetPixels.html