- Home /
How to get a Rect from Image or RectTranform?
I need to take screen shoot of specific area of my UI, so I need a Rect to use in Texture2D.ReadPixel()
.
I tried to put an Image on the RectTranform and a script like this:
Rect data=Image.GetPixelAdjustedRect();
//then draw it with
void OnGUI()
{
GUI.Box(data,"test");
}
The result :(the white box is what I'm trying to capture, you can see the darker GUI draw on the upper left)
Im also tried to use RectTranform.GetWorldCorners()
and Camera.WorldToScreenPoint() to create a Rect but no luck even it seem closer :((
Please help!
$$anonymous$$aybe it the gui.box doesn't draw the correct rect. It work well when use in texture.readpixel
Answer by anasiqbal · Jul 27, 2015 at 11:06 AM
Hi,
You can use RectTransform.rect
to get the rect of any RectTransform object. for more details see RectTransform - Unity Scripting.
NOTE: you have to be careful about one thing that it returns the "rectangle in the local space of the Transform".
Your answer
![](https://koobas.hobune.stream/wayback/20220611232959im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
RectTransform outline and position aren't synced 0 Answers
Unity UI Rect 0 Answers
Rect.Contains and Rect.Overlap always return true 1 Answer
UI How to configure Left And Right. 1 Answer