Search Unity

Get Texture Pixels using Rect coordinates

Discussion in 'Scripting' started by Rukas90, Jun 8, 2019.

  1. Rukas90

    Rukas90

    Joined:
    Sep 20, 2015
    Posts:
    169
    Hello everyone,
    I have this one problem, I want to get the texture pixels using Rect coordinates, so something like this:

    CropArea.png

    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:
    Code (CSharp):
    1. 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
     
    Last edited: Jun 8, 2019
  2. Rukas90

    Rukas90

    Joined:
    Sep 20, 2015
    Posts:
    169
    Alright, so I made a silly mistake.. Y position was alright, the only issue was the X position, which needed to be
    instead of
    to just
     
  3. Ash_Rich

    Ash_Rich

    Joined:
    Nov 16, 2017
    Posts:
    1
    I tried To fix the X position like you have however my script wont find the colours in the rect. any ideas ?
     

    Attached Files: