Get absolute UI Element position in Canvas Space

Hi,

I have a child ui element that is parented by a number of other UI elements, each with their own anchors / offsets etc. I want to find the absolute position of that particular child UI element in canvas space so that I can animate a effect to it ultimately. But I can’t find a function that easily obtains the absolute position rather than it’s rect being entirely relative to it’s parents. Does anyone have a good idea of how to achieve this?

Thanks in advance.

the rect transform has .GetWorldCorners() which will return the position of each corner. would that work for you?

2 Likes

Thanks for the response, It almost works for me, but the position seems to be slightly off (but not by a large degree), I wonder whether the canvas scaling being applied might affect this?

So yeah it turned out the canvas scalar was causing the issue.
The solution was to work out the logarithmic scale of the canvas scalar and apply it to the position returned from GetWorldCorners which allowed me to correctly find the location.

Is there any chance Unity can expose the UI logarithmic scale value from the canvasScalar rather than me having to try and calculate it manually?

can you not look at the canvas.scaleFactor?

I only use anchorMin and anchorMax, so position and width is always zero, what can I use to find the absolute anchors in canvas? Thanks.

off the top of my head there really isn’t anything but i could be wrong.