Why Font.HasCharacter() always returns true on dynamic fonts?
Asked Answered
B

2

0

Hi, basically i want to do a custom font management…

now, I have a font with limited-set of characters, take a look of this picture…

the left & right side of the picture is a set of ASCII Characters that available in a specific font, basically it work like this:

for(int y = 0; y < 16; y++) {
   for(int x = 0; x < 8; x++) {
      if(font.HasCharacter((int)( (y << 3) | x) )))
       {
          // Character is Available,
          // Label with a Round Square Border
          // with specific character will appear
        } else {
         // Just load unbordered label 
         // with default font (Arial) displayed
        }
   }
}

now this is the problem, you can see at the picture, both right & left is the same font, when this font in imported as unicode, only fonts that supported (in this case is caps character only) are displayed, which is work as expected.

now change it to dynamic fonts, as you can see the whole character is displayed (the rest unsupported is subtituted with arial, as the engine does when the font is dynamic), this not expected.

why this happen and how to fix it? so Font.HasCharacter will return true only when it available on specific font, even at dynamic font, or it is possible to turn off font fallbacks when using dynamic font?

any answer is appreciated.

Barnacle answered 6/6, 2023 at 3:30 Comment(3)

anybody else?

Barnacle

ok, the last bump,,,,

Barnacle

I'm also curious to know more about this issue. I want to resize the text based on the font used (since our fallback font is a bit bigger than the standard one) but there's no way to know if the character is being rendered using a fallback font.

Erena
B
0

Added my own temporary solution for this, i have already send this to bugfix and they confirms this, but I don’t know when it got fixed. So here @Erena, my temporary solution:

        //Booleans to determine char at index if supported
        bool[] chars;
       //This function feeds data to chars, at asset font path (Very Slow! Works on Editor Only)
        public void PopulateCharacter(string path)
        {
            //A GLITCH: Unity's Font.HasCharacter doesn't work 
            //properly on dynamic mode, we need to change it to Unicode first
            TrueTypeFontImporter fontData = (TrueTypeFontImporter)AssetImporter.GetAtPath(path);

            fontData.fontTextureCase = FontTextureCase.Unicode;
            fontData.SaveAndReimport();

            chars = new bool[128];
            for (int i = 0; i < 128; i++)
            {
                chars *=  font.HasCharacter((char)i);*

}

// Change back
fontData.fontTextureCase = FontTextureCase.Dynamic;
fontData.SaveAndReimport();
}

Barnacle answered 1/7, 2024 at 12:50 Comment(2)

Hey @WillNode, do you have any update on this case? Can't find this bug in Google and now I'm facing the same problem.

Diphenylamine

Use your eulerAnglesStored to do your rotation, but do not overwrite it in line 21 with eulerAnglesStored = transform.eulerAngles;

Mandate
O
0

I found a solution. Use

font.GetCharacterInfo(character, out var info)

it returns true if character is in the font and false if it isn’t!

Op answered 27/6, 2022 at 19:38 Comment(1)

One thing I've just realized "GetCharacterInfo" returns false if dynamic font CURRENTLY doesn't have a character. if (font.dynamic) { font.RequestCharactersInTexture(character.ToString()); } if(font.GetCharacterInfo(character, out var info)) { //Has character } Fixes this problem

Op

© 2022 - 2025 — McMap. All rights reserved.