Search code examples
c#colorsxnatexture2d

get only 1 pixel's color from a texture2D


Currently, i'm using this method to get the pixels' color of my Texture2D

    Color[] pixelColours = new Color[MyTexture.Width*MyTexture.Height];
    MyTexture.GetData<Color>(pixelColours);
  1. As you can see, I store every pixels' color in a tab.
  2. The texture2D is pretty huge : 1000 pixels x 1000 pixels.
  3. But I only need to get 1 pixel's color, it means i store 999.999 useless others pixels.
  4. The pixel's position on the Texture2D is moving, so this code is in the Update() method.

Are there others ways to get this only 1 pixel very fast with low memory cost ?


Solution

  • The GetData method is overloaded and allows you to specify a starting pixel and how many elements to get: http://msdn.microsoft.com/en-us/library/bb197093.aspx.