Find Max Line Count for Unity UI.Text

by BadmintonCat   Last Updated December 07, 2017 09:13 AM

I'm trying to determine the max line count of a UI.Text with VerticalOverflow set to truncate so that the line count can be used later. So far I came up this code which obviously is rather unpractical and perhaps even dangerous:

private int GetMaxTextLineCount()
{
    var lineCount = 0;
    var nextLineCount = 0;
    while (true)
    {
        _text.text += "\n";
        Canvas.ForceUpdateCanvases();
        nextLineCount = _text.cachedTextGenerator.lineCount;
        if (lineCount == nextLineCount) break;
        lineCount = nextLineCount;
    }
    _text.text = "";
    return lineCount;
}

Is there any better way to (pre)determine the max line count of a UI.Text component (before text is truncated at the bottom)?

Tags : unity c# gui text


Related Questions


Replace text in Unity

Updated April 03, 2016 08:05 AM



How to add a UI Text for keeping Score in Unity 5

Updated April 11, 2015 20:05 PM

Alternative to canvases (unity)?

Updated July 23, 2017 15:13 PM