1

I want to get the width of a text in unity using C# .

Here is what I am trying to do .

int GetWidthOfMessage(string message)
{
    int totalLength = 0;

    Font font = text.font; //text is my UI text
    CharacterInfo characterInfo = new CharacterInfo();

    char[] arr = message.ToCharArray();

    foreach (char c in arr)
    {
        font.GetCharacterInfo(c, out characterInfo, text.fontSize);
        totalLength += characterInfo.advance;
    }

    return totalLength;
}

But font.GetCharacterInfo(...) returns false and characterInfo.advance is 0 for any character .

8
  • According to the docs GetCharacterInfo has a FontStyle parameter. Commented Nov 10, 2016 at 13:42
  • A few things aside from the main question, couldn't you just loop through the string? it is loop-able as if it were an array of char - also you could display it and then get the size of the actual message displayed Commented Nov 10, 2016 at 13:42
  • Also from the docs - "Note: You should only ever need to use this when you want to implement your own text rendering. If the character ch with the specified size and style is present in the font texture, then this method will return true, [...]. If the character is not present, this method returns false" - so are your characters in the font texture? Commented Nov 10, 2016 at 13:44
  • I tried it with FontStyle.Normal but the result is the same , totalLength is 0 Commented Nov 10, 2016 at 13:44
  • I have my own text box and I want to expand it related to the width of the text Commented Nov 10, 2016 at 13:46

5 Answers 5

3

Apart from your original question. Following the reason you are doing all this (expanding text box according to text content).

You can use Content Size Fitter component on your text object and set Horizontal Fit property to Preferred Size. And this will solve your problem.

Update:

Add Layout Element component as well and set preferred width value to 500 for example and set Horizontal Overflow property of text to Wrap. This will work fo sure.

Sign up to request clarification or add additional context in comments.

3 Comments

this is good solution if I have only one line text , but I want to get multiple lines , for example I want maxWidth of my textbox to be 500px and after that the text should be 2 lines
1. Content Size Fitter (Horizontal Fit - Preferred Size , Vertical Fit - Unconstrained) 2. LayoutElement ( Preferred width - 500) 3. Horizontal Overflow - wrap is this all I should do?
Yes. I tested it with a text component and it works fine.
1

Try calling:font.RequestCharactersInTexture(c.ToString(), text.fontSize, text.fontStyle);

Before you call: font.GetCharacterInfo(c, out characterInfo, text.fontSize);

With the exception of '\t', I got every character I needed this way. (Probably better to request all the characters at once, though).

1 Comment

Note: this works with current UnityEditor (2020, 2021) even though according to Unity's docs it shouldn't - RequestCharactersInTexture is supposed to run asynchronously, but apparently is broken, and has to be used synchronously like this.
0

Maybe your font is dynamic: this means you have to add characters in your font, and you should use Font.RequestCharactersInTexture! This happened to me with a single UnityEngine.UI.Text component, no matter the font. Reference: https://docs.unity3d.com/ScriptReference/Font.RequestCharactersInTexture.html

Comments

0

try use : font.RequestCharactersInTexture(c.ToString());

fontSize & fontStyle use default value.

and then use : GetCharacterInfo(char ch, out characterInfo info);

Then I got the right value of characterInfo.advance

Comments

0

I found Font.RequestCharactersInTexture(mStr, fontSize, FontStyle.Normal) is ok, but Font.RequestCharactersInTexture(mStr, fontSize, FontStyle.Bold) will get character.advance = 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.