Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to get list of all characters (unicode and regular) supported by a Font?

Discussion in 'Editor & General Support' started by mikejm_, Aug 9, 2022.

  1. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    I am attempting to create my own font fallback system in UI Toolkit (Label) for multilanguage support.

    I have some stylized English fonts I really 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.
     
    Last edited: Aug 9, 2022
  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    809
  3. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
  4. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    809
    Well - you could just use TMPro to get the list of missing glyphs. When you create a font atlas, the TMPro window tells you what glyphs are missing. That could help?
     
  5. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    Someone posted a method on StackExchange which should in theory work:

    Code (csharp):
    1. Font font = Resources.Load<Font>("path");
    2.         Debug.Log("char info size " + font.characterInfo.Count());
    3.  
    4.         foreach (var item in font.characterInfo) {
    5.             Debug.Log("Font index " + item.index);//unicode value
    6.         }
    However this code doesn't work. Even with a correctly loaded Font (works for actual font purposes) font.characterInfo.Count() returns as zero.

    Any thoughts?
     
  6. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    809
    Maybe something to do with your font import settings?
     
  7. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    809
    From the manual, just in case:

     
  8. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    Thanks guys. It was an issue where if you don't set the Font to Unicode it won't give the charInfo. But if you set the Font to Unicode you can't get it to display regular plain typed characters.

    The better solution for my needs was to use Font.HasCharacter and just test each string I need character by character with it on demand.

    https://docs.unity3d.com/ScriptReference/Font.HasCharacter.html
     
  9. Neohun

    Neohun

    Joined:
    Oct 30, 2018
    Posts:
    77
    Anyone who is interested in getting the full char set of a dynamic font asset,
    there is another thread which has a solution for that: Pick all chars from TTF