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 .

user3578286
  • 147
  • 4
  • 10
  • [According to the docs](https://docs.unity3d.com/ScriptReference/Font.GetCharacterInfo.html) GetCharacterInfo has a `FontStyle` parameter. – ChrisF Nov 10 '16 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 – Alfie Goodacre Nov 10 '16 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? – ChrisF Nov 10 '16 at 13:44
  • I tried it with FontStyle.Normal but the result is the same , totalLength is 0 – user3578286 Nov 10 '16 at 13:44
  • I have my own text box and I want to expand it related to the width of the text – user3578286 Nov 10 '16 at 13:46
  • @AlfieGoodacre why should I loop through the string , and how can I get the size of the actual message displayed? – user3578286 Nov 10 '16 at 13:48
  • The string is helpful to loop through, as it avoids instantiating another object (the `char[]`) which has the exact same contents. – Alfie Goodacre Nov 10 '16 at 13:53
  • @AlfieGoodacre now I understand what you suggest , but that`s not my main problem :) – user3578286 Nov 10 '16 at 14:02

5 Answers5

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.

Umair M
  • 10,298
  • 6
  • 42
  • 74
  • 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 – user3578286 Nov 10 '16 at 14:07
  • 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? – user3578286 Nov 10 '16 at 14:31
  • Yes. I tested it with a text component and it works fine. – Umair M Nov 10 '16 at 15:21
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).

Dylan Hart
  • 19
  • 1
  • 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. – Adam May 16 '22 at 13:06
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

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

BiN
  • 11
  • 1
0

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