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

Get number of lines from new UI text?

Discussion in 'UGUI & TextMesh Pro' started by earok3d, Nov 3, 2014.

  1. earok3d

    earok3d

    Joined:
    Jan 24, 2014
    Posts:
    9
    Hi there,

    Apologies if this has already been asked/answered elsewhere for the new UI.

    In our game we've got speech bubbles (3D in scene) that use the new UI for rendering text. What we'd like to do is scale the speech bubbles depending on how many lines of text are in the bubble. After setting the 'text' field of the text object, is there any way to get back the number of lines it had to divide it into in order to make it fit?

    We'd prefer to avoid making "guesses" based on assumptions of characters-per-line and would prefer to avoid fixed-width fonts.
     
  2. ChimeraSW

    ChimeraSW

    Joined:
    Jul 24, 2013
    Posts:
    28
    text.preferredHeight

    if you need more specific, you can access the cachedTextGenerator
     
    earok3d likes this.
  3. earok3d

    earok3d

    Joined:
    Jan 24, 2014
    Posts:
    9
    Fantastic! cachedTextGenerator is exactly what I need, thanks for that.
     
    Last edited: Nov 3, 2014
  4. StefanoCecere

    StefanoCecere

    Joined:
    Jun 10, 2011
    Posts:
    210
    pay attention that cachedTextGenerator gives correct results only in the next frame

    to have instant correct values we have to call Canvas.ForceUpdateCanvases(); before accessing the cachedTextGenerator s
     
    AlexanderGolub likes this.
  5. Guy-Corbett

    Guy-Corbett

    Joined:
    Jun 30, 2013
    Posts:
    3
    Just a note for anyone else doing this. The cachedTextGenerator only seems to give a a value of greater than one of you text has a horizontal overflow of "wrap" and vertical overflow of "overflow".
     
    LiterallyJeff likes this.