Search Unity

Last letter of line is not correctly detected

Discussion in 'UGUI & TextMesh Pro' started by Aktik, Oct 22, 2021.

  1. Aktik

    Aktik

    Joined:
    Mar 28, 2020
    Posts:
    11
    Hello,

    currently I'm working on a game with a lot of text which I display in a Textbox (TextMeshPro - Text UI). Because my text appears letter by letter it results in shifting a word in a new line if it has too many letters and I don't like that effect. Therefore I try to check if a word gets truncated and if so, it gets moved by \n. Here the code:

    Code (CSharp):
    1. // A segment is the full text of a text box. textSegments is the entire dialogue
    2.         foreach (string segment in textSegments)
    3.         {
    4.             string[] words = segment.Split();
    5.             string oldTextBoxText = TextBox.text;
    6.             foreach (string word in words)
    7.             {
    8.                 TextBox.text += word;
    9.                 TextBox.ForceMeshUpdate();
    10.                 if ( TextBox.isTextTruncated ||
    11.                      TextBox.textInfo.lineInfo[TextBox.textInfo.lineCount - 1].lastCharacterIndex
    12.                     != TextBox.textInfo.lineInfo[TextBox.textInfo.lineCount - 1].lastVisibleCharacterIndex)
    13.                 {
    14.                     // Correct the overflow by "reset" the text to the old state
    15.                     TextBox.text = $"{oldTextBoxText}\n{word}";
    16.                 }
    17.                 else
    18.                     TextBox.text += " ";
    19.                 // Update the older textbox text to revert a word if its overflowing.
    20.                 oldTextBoxText = TextBox.text;
    21.             }
    22.         }
    This places the text word after word and checks if a letter is not visible (with IsTextTruncated and the lastVisibleCharacterIndex).

    The problem here is, that if only one letter is missing it doesn't detect that. Neither of the two recognize that. TextBox.PNG
    Component.PNG

    Note: I wrote "somehow" seperated on purpose to demonstrate the problem. If I write it normally it works.
    I tried also using a different Font Asset but it didn't work. I'm working with the Version 3.0.6 of TextMeshPro and Unity 2020.3.21f1.

    Do I miss something here?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I would suggest using the .maxVisibleCharacter property which controls the number of visible characters while still doing a full layout pass on the whole text thus avoiding the issue you are trying to address while at the same time avoiding using string concatenation which result in GC.

    See example 17 - Old Computer Terminal and related scripts which makes use of that .maxVisibleCharacter. This example is included in the TMP Examples & Extras.
     
  3. Aktik

    Aktik

    Joined:
    Mar 28, 2020
    Posts:
    11
    Ok I've fixed it by adding an extra "W" after each word (which gets deleted afterwards). I also noticed a mistake where text parts which should be invisible would be considered in the detection. Because of that, the text sometimes got cut on the wrong word. It seems to work fine now. I'll still look in the example to optimize it. Thank you