Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3d. How to get screen or world position of ui element

I have UI elements (image, etc). Canvas attached to camera. There is a RectTransform, but how to convert this data to screen or world coordinates and get center point of this image?

Tried RectTransform.GetWorldCorners but it returns zero vectors.

like image 899
Yura Koval Avatar asked Apr 24 '15 13:04

Yura Koval


1 Answers

yourRectTransform.rect.center for the centre point in local space.

yourRectTransform.TransformPoint to convert to world space.

It is odd that RectTransform.GetWorldCorners doesn't work as stated. Per one of the other answers you need to call after Awake (so layout can occur).

like image 190
Huacanacha Avatar answered Sep 28 '22 04:09

Huacanacha