Reputation: 8492
I'm trying to find a pixel on an image object at the relative position Vector2(50,60). But, although the object is only 100x100, my image is 1200x1200 (for resolution scaling purposes). When I try to do:
Color color = mask.sprite.texture.GetPixel(x, y);
It returns the pixel from (50,60) on the image, and not on the gameobject (50,60 on the image is always a blank pixel)
My question: how to find out the pixel color on a downscaled gameobject UI image at the desired gameobject position.
Upvotes: 1
Views: 1894
Reputation: 571
You need to calculate the position with respect to the scaling.
Vector2 originalResolution = new Vector2(1200, 1200);
Vector2 scaledResolution = new Vector2(100, 100);
Vector2 colorPixelPos = new Vector2(50, 60);
Vector2 actualPixelPos = new Vector2(colorPixelPos.x * scaledResolution.x / originalResolution.x, colorPixelPos.y * scaledResolution.y / originalResolution.y);
Color color = mask.sprite.texture.GetPixel((int)actualPixelPos.x, (int)actualPixelPos.y);
Upvotes: 3