- Home /
Predict Text Width of String
I'm trying to make an animated text box where the characters scroll across one at a time, and which fits two lines of dialogue at a time.
I want to make a function that takes in a string and breaks it up into messages that will fit nicely in the text field.
Part of doing this would be to check the width of a substring with a given font and fontSize. This is what I've done so far:
TextGenerator generator = new TextGenerator();
TextGenerationSettings settings = new TextGenerationSettings();
settings.font = Resources.Load<Font>("Fonts/MyFont");
settings.fontSize = 32
Debug.Log(generator.GetPreferredWidth("Test String", settings));
I get some value, but I'm not sure what the units are, and more importantly, this value does not change with fontSize. settings.fontSize
does nothing. I must be doing something wrong. Ideally I could eventually adapt this to get the current font and font size of the TextMeshPro component I am using, and tell it to break up the string into chunks that will fit nicely within the box.