Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

TextMesh Pro Does the Content Size Fitter work?

Discussion in 'Unity UI & TextMesh Pro' started by BlockFade, Jul 23, 2017.

  1. BlockFade

    BlockFade

    Joined:
    Jan 28, 2017
    Posts:
    24
    I'm making a dialogue bubble where it will automatically resize the bubble to the text's size.

    I have the content size fitter attached to the background image.

    But if the Text overflows, the background image doesn't change size!

    Does the content size fitter not work with TextMesh Pro?
     
  2. _Daniel_

    _Daniel_

    Joined:
    Feb 28, 2007
    Posts:
    2,615
    Try setting the vertical fit to preferred size. Unconstrained does nothing.
     
  3. BlockFade

    BlockFade

    Joined:
    Jan 28, 2017
    Posts:
    24
    It did this...
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,848
    The Content Size Fitter does work but you have to make sure you are using the TextMeshPro UGUI Component which is located in Create - UI - TextMeshPro - Text
     
  5. BlockFade

    BlockFade

    Joined:
    Jan 28, 2017
    Posts:
    24
    It is using TextMeshPro UGUI:
     
  6. MrHappyKiller

    MrHappyKiller

    Joined:
    Feb 8, 2016
    Posts:
    29
    If your content size fitter is not connected to your speech bubble sprite, it will not expand your speech bubble; only the text box.
     
  7. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,848
    You have to add some Layout component like Horizontal or Vertical Layout Group.

    upload_2017-7-22_20-14-56.png
     
    MrMatthias likes this.
  8. BlockFade

    BlockFade

    Joined:
    Jan 28, 2017
    Posts:
    24
    Thank you for your help!
    I got it working :)
     
  9. nrvllrgrs

    nrvllrgrs

    Joined:
    Jan 12, 2010
    Posts:
    11
    I'm using the same settings but my panel expands the full height of the canvas. Below are the behaviors when I change my vertical fit:
    • Unconstrained: No change
    • Min Size: Panel's height (and child TextMeshPro UGUI height) set to 0
    • Preferred Size: Panel's height (and child TextMeshPro UGUI height) set to canvas height
     
    Nodata likes this.
  10. Nodata

    Nodata

    Joined:
    Oct 29, 2014
    Posts:
    1
    Same is happening to me. Is this broken now?
     
  11. warreneng

    warreneng

    Joined:
    Mar 13, 2017
    Posts:
    8
    Sure seems like ContentSizeFitter is broken. I'm using 2018.2.12f
     
  12. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,848
    I don't recall adding a ContentSizeFitter alone on a parent object like a UI Button working. Adding either an HorizontalLayoutGroup or VerticalLayoutGroup still appears to work in addition to the ContentSizeFitter does still appear to work. See the example I posted above.
     
  13. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    513
    This may be overkill for a minor annoyance, but if you prefer, you could use this component:

    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3.  
    4. // Resizes this object based on the preferred size of a TMP Text
    5. [ExecuteInEditMode]
    6. public class TextSizer : MonoBehaviour
    7. {
    8.     public TMP_Text text;
    9.     public Vector2 padding;
    10.     public Vector2 maxSize = new Vector2(1000, float.PositiveInfinity);
    11.     public Vector2 minSize;
    12.  
    13.     public enum Mode
    14.     {
    15.         None        = 0,
    16.         Horizontal  = 0x1,
    17.         Vertical    = 0x2,
    18.         Both        = Horizontal | Vertical
    19.     }
    20.     public Mode controlAxes = Mode.Both;
    21.  
    22.     protected string lastText = null;
    23.     protected Vector2 lastSize;
    24.     protected bool forceRefresh = false;
    25.  
    26.     protected virtual float MinX { get {
    27.             if ((controlAxes & Mode.Horizontal) != 0) return minSize.x;
    28.             return GetComponent<RectTransform>().rect.width - padding.x;
    29.         } }
    30.     protected virtual float MinY { get {
    31.             if ((controlAxes & Mode.Vertical) != 0) return minSize.y;
    32.             return GetComponent<RectTransform>().rect.height - padding.y;
    33.         } }
    34.     protected virtual float MaxX { get {
    35.             if ((controlAxes & Mode.Horizontal) != 0) return maxSize.x;
    36.             return GetComponent<RectTransform>().rect.width - padding.x;
    37.         } }
    38.     protected virtual float MaxY { get {
    39.             if ((controlAxes & Mode.Vertical) != 0) return maxSize.y;
    40.             return GetComponent<RectTransform>().rect.height - padding.y;
    41.         } }
    42.  
    43.     protected virtual void Update ()
    44.     {
    45.         RectTransform rt = GetComponent<RectTransform>();
    46.         if (text != null && (text.text != lastText || lastSize != rt.rect.size || forceRefresh))
    47.         {
    48.             lastText = text.text;
    49.             Vector2 preferredSize = text.GetPreferredValues(MaxX, MaxY);
    50.             preferredSize.x = Mathf.Clamp(preferredSize.x, MinX, MaxX);
    51.             preferredSize.y = Mathf.Clamp(preferredSize.y, MinY, MaxY);
    52.             preferredSize += padding;
    53.  
    54.             if ((controlAxes & Mode.Horizontal) != 0)
    55.             {
    56.                 rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredSize.x);
    57.             }
    58.             if ((controlAxes & Mode.Vertical) != 0)
    59.             {
    60.                 rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, preferredSize.y);
    61.             }
    62.             lastSize = rt.rect.size;
    63.             forceRefresh = false;
    64.         }
    65.     }
    66.  
    67.     // Forces a size recalculation on next Update
    68.     public virtual void Refresh()
    69.     {
    70.         forceRefresh = true;
    71.     }
    72. }