Get number of lines from new UI text?

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.

text.preferredHeight

if you need more specific, you can access the cachedTextGenerator

1 Like

Fantastic! cachedTextGenerator is exactly what I need, thanks for that.

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

1 Like

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”.

1 Like