So the title is the question. I’m using the new UI system, and i cant seem to get the current size of the font when it is in “best fit” mode.
I tried to get the property “fontSize”, however, that size is not the currect one if its in the best fit mode.
Alright, i just figured it out myself.
Its in Text.cachedTextGenerator.fontSizeUsedForBestFit
if you need to get the size the same frame that you set it you can use.
text.text = "The text you want";
text.cachedTextGenerator.Invalidate();
Vector2 size = (text.transform as RectTransform).rect.size;
TextGenerationSettings tempSettings = text.GetGenerationSettings(size);
tempSettings.scaleFactor = 1;//dont know why but if I dont set it to 1 it returns a font that is to small.
if(!text.cachedTextGenerator.Populate(textVal, tempSettings))
Debug.LogError("Failed to generate fit size");
text.resizeTextMaxSize = text.cachedTextGenerator.fontSizeUsedForBestFit;
Here’s my trick:
private int mFontMaxAtual;
public int mBuggedSize;
public int mCorrectFont;
public int fontSizeAtual;
// Use this for initialization
void Awake ()
{
mFontMaxAtual = GetComponent<Text> ().resizeTextMaxSize;
}
// Update is called once per frame
void Update ()
{
fontSizeAtual = GetComponent<Text> ().cachedTextGenerator.fontSizeUsedForBestFit;
if (fontSizeAtual == mBuggedSize) {
GetComponent<Text> ().resizeTextMaxSize = mCorrectFont;
} else {
GetComponent<Text> ().resizeTextMaxSize = mFontMaxAtual;
}
print (GetComponent<Text> ().cachedTextGenerator.fontSizeUsedForBestFit);
}