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

Fit to size of UI element with child Text, if child text transform is scaled?

Discussion in 'UGUI & TextMesh Pro' started by mgear, Feb 9, 2017.

  1. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,988
    So is there some way to scale panel to fit child text size, if the text object is scaled?
    (and text gameobject has to be scaled to get smoother font..)

    here's my hierarchy:

    Canvas
    - Panel < this should scale with text size, and it works correctly, but only if text recttransform scale is 1,1,1
    - - Text

    upload_2017-2-9_13-51-7.png

    https://docs.unity3d.com/Manual/HOWTO-UIFitContentSize.html
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,076
    AFAIK there is no solution out of the box for your problem... so you would have to write your own content size fitter (maybe you can extend the existing one).

    However, it is almost always a bad idea to scale UI elements. It leads to unexpected behavior in many cases. In the very most cases there is a way to achieve your goals without scaling.
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @mgear

    Hi there, @Hosnkobf might be right...

    I see you have used stretched panel, so it's one case. Then again, if you have not split anchors... it's different.
    Maybe just do it with code?

    I tried to solve it, have done something similar as a practice. It's crude but should work. Text can be scaled, I didn't take into account moving pivots or parent scaling... but this way panel should be fitting text rect that is child of this panel.

    NOTE: this only works if child text is centered unlike your pivot, that is in upper left corner. Stretched text won't work either...

    Code (csharp):
    1.  
    2. // Dimensions
    3. sizeFinalX = textRt.sizeDelta.x * textRt.localScale.x;
    4. sizeFinalY = textRt.sizeDelta.y * textRt.localScale.y;
    5.  
    6. splitAnchors = Vector3.Distance(panelRt.anchorMin, panelRt.anchorMax) > 0.001f;
    7.  
    8. // Anchors split... above not working, use this
    9. if (splitAnchors)
    10. {
    11.   splitAnchors = true;
    12.   panelRt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, sizeFinalX);
    13.   panelRt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, sizeFinalY);
    14. }
    15. else
    16. {
    17.   // Anchors not split, center pivot
    18.   panelRt.sizeDelta = new Vector2(sizeFinalX, sizeFinalY);
    19. }
    20.  
    ...maybe it's helpful... it does not use scaling, instead panel is resized.
     
    mgear likes this.
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,988
    Thanks! Using script would be fine, since i'm settings the text from script anyways..

    but got it actually working, had forgotten that can set the canvas dynamic pixel scale instead of scaling text..(to get sharp font), so can remove scaling from text component.

    here's working setup
    upload_2017-2-10_9-31-40.png

    *Update, using 2019.4.x
    upload_2022-9-21_10-43-22.png
     
    Last edited: Sep 21, 2022
    Fattie likes this.
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,988
    Blast from the past.. had this issue today, found another workaround:
    - Add dummy gameobject before the panel AND scale that down
    - then you dont need to have scaled text or panel, which means: content size fitter and layout group will resize the panel correctly to the child text size.
     
  6. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    475
    @mgear your tip on "change dynamic pixels" is incredibly helpful, wish I could send you a million points!
     
  7. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    475
    For the record, @Hosnkobf mentioned

    "However, it is almost always a bad idea to scale UI elements."

    While that is true for anything other than text, in Unity, you have to do it to text almost every time since the text engine is comic.

    (Usually you just make the point size 10x what you actually want, and, then make the scale .1)
     
  8. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,076
    Or you simply use TextMesh Pro for all labels in your UI.