I have int values that i want to display in TextMeshPro elements.
I turned off AutoSize on all TMP elements to save performance, so i’ve to pre-calculate the fontSize values depending on the number of characters displayed.
Whenever a value changes in any TMP, i check the character count and i just assign the pre-calculated value as fontSize for that count.
To get the number of characters i’m using this:
private void SetFontSize(string str)
{
var length = str.Length;
if(length == 1)
{
a.fontSize = oneCharFontSize;
}
else if(length == 2)
{
a.fontSize = twoCharFontSize;
}
else if(intStr == 3)
{
a.fontSize = threeCharFontSize;
}
else if(intStr == 4)
{
a.fontSize = fourCharFontSize;
}
.....
}
Is it better this way or should i use:
private void SetFontSize(string str)
{
int intStr = (int) str;
a.fontSize = oneCharFontSize;
if(intStr > 9)
{
a.fontSize = twoCharFontSize;
}
else if(intStr > 99)
{
a.fontSize = threeCharFontSize;
}
else if(intStr > 999)
{
a.fontSize = fourCharFontSize;
}
.....
}
Which one is less expensive if i’m doing this A LOT ?