Search Unity

Resolved How to update a RectTransform's AnchoredPosition when its pivot is changed

Discussion in 'UGUI & TextMesh Pro' started by Tuonela93, Jan 18, 2024.

  1. Tuonela93

    Tuonela93

    Joined:
    Apr 7, 2018
    Posts:
    27
    Asking this here since I cannot find a forum related to UI that is alive.

    I am changing the pivot of a RectTransform at runtime and want to take the new anchoredPosition that results from this change and use it for tweening.

    However the value I get from panel.anchoredPosition.y is still the old anchoredpos. I tried waiting for 1-2 frames before asking for the value, I tried panel.ForceUpdateRectTransforms(), no result.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,685
    I would simply author a separate invisible GameObject that you use as the pivot.

    The reason is that manipulating RectTransforms is almost always a complicated disaster.

    For instance it is some 15 lines of code just to set the anchors to the outside of the RectTransform.

    http://forum.unity3d.com/threads/sc...hor-to-gui-object-size-rect-transform.269690/
     
    Tuonela93 likes this.
  3. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    329
    because changing the pivot doesnt change the anchored position

    it does in the editor but that is because the editor is wacky

    what you need is a formula that copies the wacky behaviour of the unity editor

    with that being said when the moderators find this thread they are gonna send u back to the shadow realm
     
    Tuonela93 likes this.
  4. Tuonela93

    Tuonela93

    Joined:
    Apr 7, 2018
    Posts:
    27
    Kurt, I did it the way you described and it worked out. Thanks!
     
    Kurt-Dekker likes this.