As per Unity’s recommendations for new projects for ongoing support, I am converting everything over to use TextMeshPro, but I’ve hit a bit of a wall.
I have a system that requires getting the width and height of the text space in character length.
Until now, I’ve been using the GetGenerationSettings using the following methods to calculate the maximum line length and height, but this settings method is absent in the TextMesh class and I can’t find any alternatives online.
static int GetMaxLineCount(Text text)
{
var textGenerator = new TextGenerator();
var generationSettings = text.GetGenerationSettings(text.rectTransform.rect.size);
var lineCount = 0;
var s = new StringBuilder();
while (true)
{
s.Append("\n");
textGenerator.Populate(s.ToString(), generationSettings);
var nextLineCount = textGenerator.lineCount;
if (lineCount == nextLineCount) break;
lineCount = nextLineCount;
}
return lineCount;
}
static int GetMaxLineLength(Text text)
{
var textGenerator = new TextGenerator();
var generationSettings = text.GetGenerationSettings(text.rectTransform.rect.size);
var charCount = 0;
var s = new StringBuilder();
while (true)
{
s.Append("A");
textGenerator.Populate(s.ToString(), generationSettings);
var nextLineCount = textGenerator.characterCount;
if (charCount == nextLineCount) break;
charCount = nextLineCount;
}
return charCount;
}
Is there a built-in method that returns the TextGenerationSettings or am I out of luck? Should I just stick with the legacy Text component for this?