Search Unity

Using Content size fitter to scale parent image with child text

Discussion in 'UGUI & TextMesh Pro' started by mrCharli3, Jan 26, 2020.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I wan to scale my parent image with my child text. To do this I add a Content size fitter to both parent and child, as well as a horizontal layout group to the parent. This works, however I get this warning on the child:

    warning.PNG

    But, if I try to do as the warning says and remove the Content size fitter from my text, the parent stops scaling with the text. Is this some bug or am I missing something?

    Also, is there some way I can add linebreaks automatically after X amount of characters?
     
  2. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    Add a LayoutGroup to the child also, and remove its CSF. And make sure both LayoutGroups have Child Controls Size ticked on Width and Height.

    Automatically, no. You'll need to code it:
    Code (CSharp):
    1.     [RequireComponent(typeof(Text))]
    2.     public class TextAutoLineBreak : MonoBehaviour
    3.     {
    4.         [SerializeField]
    5.         int _CharactersPerSection = 100;
    6.  
    7.         Text _Text;
    8.         string _PrevKnownText;
    9.         int _PrevKnownCharactersPerSection;
    10.  
    11.  
    12.         void Start()
    13.         {
    14.             _Text = GetComponent<Text>();
    15.         }
    16.  
    17.         void Update()
    18.         {
    19.             if (_Text.text == _PrevKnownText && _PrevKnownCharactersPerSection == _CharactersPerSection)
    20.                 return;
    21.  
    22.             char[] chars = _Text.text.ToCharArray();
    23.             var sb = new System.Text.StringBuilder();
    24.             int lastNLIndex = -1;
    25.             int lastAppendedCharIndex = -1;
    26.             for (int i = 0; i < chars.Length; i++)
    27.             {
    28.                 if (chars[i] == '\n')
    29.                 {
    30.                     lastNLIndex = i;
    31.                     continue;
    32.                 }
    33.  
    34.                 int charsInSection = i - lastNLIndex;
    35.                 if (charsInSection == _CharactersPerSection)
    36.                 {
    37.                     int start = lastAppendedCharIndex + 1;
    38.                     sb.Append(chars, start, i - start);
    39.                     lastAppendedCharIndex = i;
    40.  
    41.                     sb.Append('\n');
    42.                     lastNLIndex = i;
    43.                 }
    44.             }
    45.             if (lastAppendedCharIndex < chars.Length - 1)
    46.             {
    47.                 int start = lastAppendedCharIndex + 1;
    48.                 sb.Append(chars, start, chars.Length - start);
    49.             }
    50.             _PrevKnownText = _Text.text = sb.ToString();
    51.             _PrevKnownCharactersPerSection = _CharactersPerSection;
    52.         }
    53.     }
     
    mrCharli3 likes this.
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Are you sure you want linebreaks every X characters, or do you want the text to wrap when it gets to the end of the available space? Those are not the same, for a few different reasons.
     
  4. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Perfect thank you!
     
    xucian likes this.