Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Get absolute UI Element position in Canvas Space

Discussion in 'UGUI & TextMesh Pro' started by matthewn, Jan 15, 2015.

  1. matthewn

    matthewn

    Joined:
    Dec 15, 2014
    Posts:
    7
    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.
     
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    the rect transform has .GetWorldCorners() which will return the position of each corner. would that work for you?
     
    akshay-skeletoncrew and matthewn like this.
  3. matthewn

    matthewn

    Joined:
    Dec 15, 2014
    Posts:
    7
    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?
     
  4. matthewn

    matthewn

    Joined:
    Dec 15, 2014
    Posts:
    7
    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?
     
  5. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    can you not look at the canvas.scaleFactor?
     
  6. Cuku_

    Cuku_

    Joined:
    Sep 2, 2012
    Posts:
    10
    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.
     
  7. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    off the top of my head there really isn't anything but i could be wrong.