I have an object with a RectTransform component
I want to get a rect in screen space of that object
so that would be in pixel values
can consider that the object is not out of camera view if needed
I have an object with a RectTransform component
I want to get a rect in screen space of that object
so that would be in pixel values
can consider that the object is not out of camera view if needed
welp
banished to the shadow realm
yet still one of the top posts on googles stupid SEO. yay
i found out how to do it if you want
not like the mods helped by moving this out of the scripting forum
Yeah I’d love to know! That’s for responding btw
// Get the corners of the RectTransform in world space
Vector3[] corners = new Vector3[4];
rectran.GetWorldCorners(corners);
// Convert world space to screen space in pixel values and round to integers
for (int i = 0; i < corners.Length; i++)
{
corners[i] = cam.WorldToScreenPoint(corners[i]);
corners[i] = new Vector3(Mathf.RoundToInt(corners[i].x), Mathf.RoundToInt(corners[i].y), corners[i].z);
}
// Calculate the screen space rectangle
float minX = Mathf.Min(corners[0].x, corners[1].x, corners[2].x, corners[3].x);
float minY = Mathf.Min(corners[0].y, corners[1].y, corners[2].y, corners[3].y);
float width = Mathf.Max(corners[0].x, corners[1].x, corners[2].x, corners[3].x) - minX;
float height = Mathf.Max(corners[0].y, corners[1].y, corners[2].y, corners[3].y) - minY;
// Display the screen space rectangle
Rect screenRect = new Rect(minX, minY, width, height);
Thanks man