Search Unity

Unity UI Converting between RectTransform coordinates

Discussion in 'UGUI & TextMesh Pro' started by Senshi, Jul 12, 2017.

  1. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    Hi all,

    I have a UI layout that looks like this:

    -- Panel A
    ---- GridLayout
    ------- Elements
    -- Panel B

    Both panels use the anchors to position themselves on the canvas, with offsets of (0, 0, 0, 0). Panel B is a full (0, 0, 1, 1) overlay.

    What I want now is to spawn a new UI prefab (again using anchored positions) onto panel B, at the center position of one of the layout-controlled Elements.

    However, I can't seem to figure out how to obtain this calculated position in "Panel B space". I'm already waiting until the layout is completed with a WaitForEndOfFrame. Any thoughts?

    Thanks!
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    I would have thought the easiest way would be to get the real world coordinates of the Grid Element using "GetWorldCorners", then use that to calculate the anchor / pivot position for the new element, translate it back to screenspace and then parent it to panel B.

    The ReorderableList in the UI Extensions does something very similar I believe.