- Home /
C# - how do I get the number of lines in a TextMesh
i've found information concerning TextGenerator, but this just seems way too complicated for such a simple thing. I would like to find the exact number of lines on a TextMesh, counting the horizontal overflow. I just want there to be something like: TextMesh.CountLines or something.
I have a GameObject with a TextMesh component and whenever I change the text, I want to then count the lines and change a spriteRenderer to have a speechBubble appropriate in size. Is this easy to do or do i need to go and figure out this TextGenerator thing?
Answer by EDevJogos · Jan 25, 2017 at 02:53 AM
textMesh.text.Split('\n').Length
That solved the issue, but I just realised i can't put a rect transform on a text$$anonymous$$esh, so it doesn't have wrapping enabled. I was assu$$anonymous$$g it would work like a UI Text. - There isn't by any chance, a way to have a Text $$anonymous$$esh operate like that is there?
Not thtat i know of, but you can split the text into words, and then set a limit of how many characters can go into a line, count how many character the words have and based on the limit dived them into the lines.
Doesn't seem to respect the new lines added by text wrapping
Answer by noboRenelieSalazar · Aug 16, 2021 at 08:34 AM
You can use textInfo.lineCount
from your TextMeshPro reference.
This actually saved my day, especially since GetTextInfo(string text) forces TMP to recalculate it's dimensions (I needed it's size right after setting text)~ thanks ^^