Search Unity

ContentSizeFitter - Get Dimensions In Script

Discussion in 'UGUI & TextMesh Pro' started by JAKJ, Aug 25, 2014.

  1. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    I am using a ContentSizeFitter on a Text object, but the sizeDelta property of the RectTransform is not being updated by the ContentSizeFitter so I can't read the dynamic height. How can I determine the current dimensions of the RectTransform at runtime of a UI element being adjusted by ContentSizeFitter?
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    The sizeDelta property of the RectTransform is updated by the ContentSizeFitter. However, it only happens at the end of a frame (right before rendering) so if you're querying it from script right after changing something that would affect it, the result won't be correct yet.

    You can invoke Canvas.ForceUpdateCanvases to have the new size calculated right away, though it has some overhead, so shouldn't be invoked each frame.
     
    McBeer likes this.
  3. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    Awesome, that worked.