3
\$\begingroup\$

I am attempting to create my own font fallback system for multilanguage support using UI Toolkit in Unity (Label class). This UI system does not intrinsically support fallback fonts.

I have some stylized English fonts I like which will be primary my first choice for some purposes. I want to make a list of all the unicode/regular characters in each of those fonts.

Then when I try to update a Label with a new string, I want to test the string to make sure it doesn't have any characters outside the list supported by the intended "stylized" English Font. ie. Test each character of the new string against my lists of supported characters.

If the new string has a character outside that list, I will instead assign a more generic backup font which covers most of the full Unicode range.

I see a script here for creating lists of glyphs within a font:

https://stackoverflow.com/questions/61573165/how-to-get-all-unicode-characters-supported-by-fonts

However, I cannot use it in Unity as it seems to rely on System.Windows.Media which is not available in my Visual Studio from Unity.

Is there any default way of doing this for a given Font that you have loaded into Unity? Thanks.

\$\endgroup\$
1

1 Answer 1

2
\$\begingroup\$

See Font-characterInfo.

var font = Resources.Load<Font>("the path of your font file under Assets dir");
foreach (var item in font.characterInfo)
{
    Debug.Log(item.index);//unicode value
}
\$\endgroup\$
2
  • \$\begingroup\$ Thanks Mangata, I can see how this would theoretically work, but for some reason it is not. I am accessing the font just as you say (this is how I usually load them) but it is returning Font font = Resources.Load<Font>("Fonts & Materials/Montserrat-Bold"); Debug.Log("char info size " + font.characterInfo.Count()); as zero. Ie. there is nothing in characterInfo. I tried this with a few fonts and same thing. Any ideas or solutions? \$\endgroup\$
    – mike
    Commented Aug 10, 2022 at 2:03
  • \$\begingroup\$ @mike Try to set Character option to Unicode instead of Dynamic in the inspector of the font asset. see class-Font. \$\endgroup\$
    – Mangata
    Commented Aug 10, 2022 at 8:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .