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

UI.Text - how can I check for line wrapping?

Discussion in 'UGUI & TextMesh Pro' started by Dreamwriter, Oct 3, 2014.

  1. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    I'm making a text box fill up character by character, how can I check to see if the next word I'm adding to the text is going to be wrapped to the next line? I tried just adding the next word all at once and comparing text.cachedTextGenerator.lineCount to what it was before adding the word, but that value doesn't seem to update until the text has been rendered.
     
    Last edited: Oct 7, 2014
  2. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    Anybody have any ideas? It looks really ugly to start writing a word letter by letter and then seeing it drop the whole word to the next line. Other text systems have a function you can call to see how many pixels a word will take up when rendered, and you can get the current length of the current line, so calculate it that way. But I can't figure out how to do that here.
     
  3. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    There is a protected field on InputField called: cachedInputTextGenerator and a public one on text called: cachedTextGenerator

    This is a text generator that is used to generate the vertices for display. After generation you can look at the 'characters' field and 'lines' field. This will give you information about the layout of the characters.
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,870
    The easiest way to make text appear character by character is to have all the text present from the beginning but use rich text to make the hidden part have a color with alpha 0.
     
    vvnurmi likes this.
  5. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    You could also check out TextMesh Pro which has a property TextMeshPro.maxVisibleCharacters which controls the number of characters shown. This works with word wrapping, text alignment, etc. In addition, you don't need to manipulate the text via rich tags.

    Here is an example where the text is static and revealed by controlling this maxVisibleCharacters property.
     
  6. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Sort of similar to the above method, here are two more examples of using a simple script to either animated vertex colors per character or vertices. In both instance, word wrapping, text alignment and all those features are still handled as expected.





    P.S. Both of these methods are super efficient since we are only changing the vertex color or vertex positions. Note also the vertex color gradient, outline and soft shadow.
     
    AntFitch likes this.
  7. Hash-Buoy

    Hash-Buoy

    Joined:
    Jan 4, 2014
    Posts:
    33
    Hey Tim, I tried out your solution but the number of lines which are returned does not match the text wrapped line.
    I tried folowing things mostly they are the same tho
    - Debug.Log ("number of lines " +mainTextField.cachedTextGenerator.lines.Count);
    - Debug.Log ("number of lines " +mainTextField.cachedTextGenerator.lineCount);

    I see 5 lines on screen but the debug returns 8 , there is no relation at all.
     
  8. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,196
    Bump, I'm having the same problem as Hash Buoy...
     
  9. AlfonsoC

    AlfonsoC

    Joined:
    Oct 1, 2014
    Posts:
    27
    did you check the height of the UI Text RectTransform?
     
  10. maxizrin

    maxizrin

    Joined:
    Apr 13, 2015
    Posts:
    17
    Canvas.ForceUpdateCanvases();
    Should get you the correct values(this seems to be off sometimes for different font?!)
     
  11. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Here is a revised example of using TextMesh Pro in conjunction with the .maxVisisbleCharacters property which controls how many characters are shown.

    In this example, the input text (string) is never modified. This works with all text alignments like Justified for example, works will all rich text tags and word wrapping behaves as expected.

    This is an example I produced for a user looking to simulate a computer terminal.

    TextMesh Pro - Computer Terminal.gif
     
    Rodolfo-Rubens likes this.
  12. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    We've been having similar issues, so we decided to make a package with various text effects for the standard Unity UI text. One of those effects is Limit Visible Characters, which lets you define, how many characters should be visible. It doesn't affect line wrapping, and you don't have to add character by character to the text - you only need to change a single variable on the Limit Visible Characters effect (a typewriter sample is included). Here's a link to the package:

    https://www.assetstore.unity3d.com/en/#!/content/52508
     
    JoeStrout likes this.