- Home /
Get "CanvasSpace" position of UI element
I have an UI element on an UI canvas. When this UI moves, I need to keep it inside certain limits - a section of the canvas.
The Canvas is in "Screen Space - Overlay" mode and it uses Canvas Scaler, so I would like to avoid calculating back and forth from World Space all together. Also I would like to avoid Screen Space, as it seems to clash with Canvas Scaler.
The UI element I want the position of, is quite deeply nested.
My question is: Can I get the "CanvasSpace" of the UI element somehow?
That is, something like the rectTransform.anchoredPosition just absolute (to the Canvas), instead of relative to it's parent. Is that possible? Any input are most welcome!
Answer by DustyScreen · Jul 24, 2015 at 12:42 PM
Found a solution myself:
public Transform canvasTransform; //remember to set the Canvas' Transform in the inspector
void Update ()
{
Debug.Log (canvasTransform.InverseTransformPoint (transform.position));
}
This seems to do the trick. I can imagine that InverseTransformPoint is quite heavy... so more lightweight solutions are very welcome! :-)