Search Unity

Resolved Get element's center position

Discussion in 'UI Toolkit' started by RunninglVlan, Jul 14, 2021.

  1. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    182
    In uGUI we could center element's pivot and when calling
    transform.localPosition
    we'd get the correct position, how to do it in UITK?
    transform.position
    returns position at upper left corner (and I haven't changed Pivot Origin).
     
    Last edited: Jul 14, 2021
  2. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    182
    I thought I could add a child which is placed at the center of parent and read its position, but its
    transform.position
    returns (0, 0) =(
     
  3. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    182
    Alright, I searched the forum some more and found that it can be achieved using
    worldBound.center
    , it was NaN when I wanted to use it, so I waited until layout was calculated using
    element.RegisterCallback<GeometryChangedEvent>
     
    Midiphony-panda and SimonDufour like this.