Search code examples
c#unity-game-enginetexturesgetpixel

Get Texture Pixels using Rect coordinates


I have this one problem, I want to get the texture pixels in Unity using Rect coordinates, so something like this:

enter image description here

Get all the pixels from the marked area. I know that x: 0 and y: 0 is at the bottom left corner, when you're trying to get the pixels, so I tried to get the pixels with these rect coordinates:

Color[] pixels = texture.GetPixels(texture.width - (crop.width + crop.x), texture.height - (crop.height + crop.y), crop.width, crop.height);

But unfortunately it doesn't work properly. I'm not sure what I'm doing wrong here, can someone give me some advice on how to solve this issue? Thank you


Solution

  • Alright, so I made a silly mistake.. Y position was alright, the only issue was the X position, which needed to be instead of

    texture.width - (crop.width + crop.x)

    to just

    crop.x