Search Unity

What's the right font can be used in UI Toolkit?

Discussion in 'UI Toolkit' started by YinZuoyu, Sep 8, 2020.

  1. YinZuoyu

    YinZuoyu

    Joined:
    Aug 25, 2015
    Posts:
    44
    I find some font in Windows.Their suffix are ttf,ttc or fon.But they couldn't be used.What's the right font can be used in UI Toolkit?
     
  2. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi YinZuoyu!

    I would be glad to help you. Could you be more specific regarding your use case and the font files you couldn't use?

    Currently, UI Toolkit should support all font files that are supported by Unity, namely .ttf, .otf and .ttc.
     
  3. YinZuoyu

    YinZuoyu

    Joined:
    Aug 25, 2015
    Posts:
    44
    New
    I think it is version trouble.The font is supported in 2020.1.3.But I used 2020.2.0a21.
     
  4. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    240
    Hi.

    In my case in ui builder font list is always empty. I'm using unity 2020.1.4, URP and latest UIToolkit pachages.

    upload_2020-9-9_13-10-35.png

    If I do same in debugger I get the list populated

    upload_2020-9-9_13-13-50.png

    Is it a bug, or as designed?
     
    Last edited: Sep 9, 2020
  5. YinZuoyu

    YinZuoyu

    Joined:
    Aug 25, 2015
    Posts:
    44
    Thanks.I get it.
     
  6. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    The font picker, when launched from the UI Builder, has the package assets hidden by default (the little eye icon on the top right). Whereas the debugger has the package assets visible by default.

    I think the reasoning behind this is that users should not use package assets in their UI.
     
  7. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    240
    Clicking on eye icon does nothing in my case and those are imported fonts inside asset folder, not fonts from packages.
     
    HugoBD-Unity likes this.
  8. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Thank you for bringing this up Maverick, I just tested and it doesn't work for me either! Do you mind reporting it via Unity's built-in Bug Reporter?
     
  9. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    240
  10. Twanner

    Twanner

    Joined:
    Dec 6, 2014
    Posts:
    33
    Thanks for reporting this, I'm having the same problem
     
  11. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    could we get an update on this?
     
    FizzFab likes this.
  12. nobeerleft

    nobeerleft

    Joined:
    Mar 29, 2012
    Posts:
    27
    I love how QA say they cant repro this while using a way older version of UI Toolkit (1.0.0-preview.1)
     
    mowax74 and thelebaron like this.
  13. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    240
  14. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    In preview14 I can indeed select my fonts, with the following caveats:

    - I need to make my Font Character Dynamic and my Font Asset Atlas Population Mode Dynamic (the latter done automatically when creating TMP Font Asset from a dynamic font) for my text to appear in UI Builder or at runtime (using UI Document).
    - I can select a Static font in the UI Builder, but the text will be invisible, and apparently shrinked to size 0 as the container also shrinks.
    - When creating a UI Toolit > Text Settings (UITK Text Settings aka Panel Text Settings), the Default Font Asset > Default Font Asset can only be filled with a (non-TMP) Font Asset. Static fonts don't allow creating a non-TMP Font Asset, so I had to resort to Dynamic Font again.

    As a result, I'm using a dynamic font everywhere, which is OK but for pixel art fonts they appear blurry on the edges... Setting Atlas Render Mode to RASTER_HINTED doesn't help.
     
  15. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @huulong!

    Could you be more specific on how you created your FontAsset? FontAssets created from the MenuItem `Create/Text/FontAsset` should be `AtlasPopulationMode.Dynamic` by default.

    You shouldn't have an issue using a static FontAsset in the UIBuilder. Make sure the atlas is prepopulated with the relevant characters. To do so, use the FontAsset Creator Window. It is located in Window/Text/Font Asset Creator. I also recommend having a look at this documentation, it explains in more depth how to do so. Just keep in mind that it was made for TextMesh Pro, not for TextCore. https://learn.unity.com/tutorial/textmesh-pro-font-asset-creation#5f60578fedbc2a0022b9899d

    As for Pixel Art fonts, could you explain what you are trying to achieve and share your current setup (Font, FontAsset you are using...)?
     
    Last edited: May 20, 2021
  16. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
  17. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    When creating a font this way from an original Font asset imported as Dynamic, the resulting Unity Font asset is set to Dynamic indeed. But I needed to change the import mode first.

    I made sure to create the Atlas with the characters I needed (not sure if it was ASCII or Unicode). As a matter of fact, I can use the font on TMP text. Because I only needed certain font sizes, I made it static at first. I can also use dynamic font with TMP. But I have no choice for the UI Toolkit.

    That said, I generated the Atlas a while ago, and it wasn't for the UI Toolkit. So I can try to generate them again and testing live within my UITK template. The tutorial and thread seems like what I did and what I've read in the documentation, but I'll try again following the steps more closely.

    I'm using the BitPotion Ext font family. When I get back on my project I can give you more details on the import settings, and upload a screenshot of the crisp vs blurry font rendering.
     
  18. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    Ah, sorry, actually only when creating the Custom UITK Text Settings (Panel Text Settings) I was constrained to selecting the dynamic non-TMP font asset for the default font field (see first screenshot).

    So I tried to create a new non-TMP Font Asset from BitPotionExt.ttf, but clicking on Generate Font Atlas gave an error:

    Font Asset Creator - Error Code [Invalid_File] has occurred trying to load the [BitPotionExt] font file. This typically results from the use of an incompatible or corrupted font file.
    UnityEngine.GUIUtility: ProcessEvent (int,intptr,bool&) (at /home/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)

    If instead I try the shortcut for quick creation of right-click Create > Text > Font Asset, I get:

    Unable to load font face for [BitPotionExt]. Make sure "Include Font Data" is enabled in the Font Import Settings.
    UnityEditor.TextCore.FontAsset_CreationMenu:CreateFontAsset () (at Library/PackageCache/com.unity.textcore@1.0.0-preview.2/Scripts/Editor/FontAssetCreationMenu.cs:170)

    Setting the font asset import setting Character from Unicode to Dynamic (with Include Font Data) makes Font Asset generation work with both techniques. And actually, the Include Font Data setting is only present for Dynamic fonts. So the font file is not corrupted (I tried, it does the same with other fonts), the generator itself, with the shortcut command, is asking me to make a dynamic font.

    (You can get the font for free at https://joebrogers.itch.io/bitpotion if you want to test by the way, if you need to test rasterization; but any pixel art font will do)

    And on top of that, none of this seems to matter because in the UI Builder, it is not possible to clear the Text font field to use the default provided by Panel Text Settings. Trying to set it to None or pressing Delete does nothing and shows this log:

    UI Builder: Font cannot be set to none.
    UnityEngine.Debug:Log (object)
    Unity.UI.Builder.BuilderInspectorStyleFields:OnFieldValueChangeFont (UnityEngine.UIElements.ChangeEvent`1<UnityEngine.Object>,string) (at /home/bokken/buildslave/unity/build/External/MirroredPackageSources/com.unity.ui.builder/Editor/Builder/Inspector/BuilderInspectorStyleFields.cs:1773)
    Unity.UI.Builder.BuilderInspectorStyleFields/<>c__DisplayClass28_0:<BindStyleField>b__11 (UnityEngine.UIElements.ChangeEvent`1<UnityEngine.Object>) (at /home/bokken/buildslave/unity/build/External/MirroredPackageSources/com.unity.ui.builder/Editor/Builder/Inspector/BuilderInspectorStyleFields.cs:192)
    UnityEngine.GUIUtility: ProcessEvent (int,intptr,bool&) (at /home/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)

    So, I'm constrained to picking the text on each Label element or using a style. Which is fine for me. However, the Label > Text > Font field only takes pure font assets like .ttf. So no Font Asset generation at all here. I can only pick my Unicode or my Dynamic-imported font file.

    Picking the Dynamic font works but is blurry. Picking the Unicode or any other static set font spams two errors:

    Unable to load font face for [SourceSansPro-Black_STATIC]. Make sure "Include Font Data" is enabled in the Font Import Settings.
    UnityEngine.UIElements.UIElementsRuntimeUtilityNative:UpdateRuntimePanels () (at /home/bokken/buildslave/unity/build/Modules/UIElementsNative/UIElementsUtility.bindings.cs:26)

    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.TextCore.Text.TextSettings.GetCachedFontAssetInternal (UnityEngine.Font font) (at Library/PackageCache/com.unity.textcore@1.0.0-preview.2/Scripts/Runtime/TextSettings.cs:268)
    UnityEngine.UIElements.Text.PanelTextSettings.GetCachedFontAsset (UnityEngine.Font font) (at Library/PackageCache/com.unity.ui@1.0.0-preview.14/Text/PanelTextSettings.cs:42)
    UnityEngine.UIElements.Text.TextUtilities.GetFontAsset (UnityEngine.UIElements.MeshGenerationContextUtils+TextParams textParam) (at Library/PackageCache/com.unity.ui@1.0.0-preview.14/Text/TextUtilities.cs:32)
    ...
    UnityEngine.Yoga.Native.YGNodeMeasureInvoke (UnityEngine.Yoga.YogaNode node, System.Single width, UnityEngine.Yoga.YogaMeasureMode widthMode, System.Single height, UnityEngine.Yoga.YogaMeasureMode heightMode, System.IntPtr returnValueAddress) (at /home/bokken/buildslave/unity/build/External/Yoga/csharp/Facebook.Yoga/YogaNative.bindings.cs:146)
    UnityEngine.UIElements.UIElementsRuntimeUtilityNative:UpdateRuntimePanels() (at /home/bokken/buildslave/unity/build/Modules/UIElementsNative/UIElementsUtility.bindings.cs:26)

    (see second screenshot)

    and the text is invisible in the UI preview.

    That's it. Is there any way I can use a rasterized font in UI Toolkit menu?
    I can export a Dynamic font in RASTER_HINT mode as TextCore Font Asset, but since I cannot select it in the Label Text Font field, it doesn't help.

    Note that I'm giving up on making my menu with UITK for now because of another issue, namely failing keyboard/gamepad navigation (at least on Linux). I have reported this separately.

    P.S.: we need a checkbox to disable emoji generation, all callstack containing :process end up creating a smiley!
     
  19. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @huulong !

    This is indeed confusing, but TMP_FontAsset created from TextMeshPro cannot be used in UIToolkit. For the moment, UIToolkit only supports assets created from TextCore. We are planning to migrate the TMP assets to TextCore, but this is more complicated than expected, and it will not happen shortly... When you create your assets, make sure to use the Text/ menu item rather than the TextMeshPro/ menu item.

    Unable to load font face for [BitPotionExt]. Make sure "Include Font Data" is enabled in the Font Import Settings.

    This is expected, for the Font to be consumable by TextCore, the unity font settings should be the default one. (see screenshot). You should then be able to create a static FontAsset from this unity Font using the Window/Text/Font Asset Creator.

    UI Builder: Font cannot be set to none.

    This is a known issue that we should soon fix.

    That's it. Is there any way I can use a rasterized font in UI Toolkit menu?
    I can export a Dynamic font in RASTER_HINT mode as TextCore Font Asset, but since I cannot select it in the Label Text Font field, it doesn't help.


    From your screenshot, it seems you are not using the UIBuilder package. Using the package in versions earlier than 21.2 will allow you to use FontAssets.

    it is not possible to clear the Text font field to use the default provided by Panel Text Settings.
    PanelTextSettings and PanelSettings are currently not available in the UIBuilder. This is unfortunate, but it is on the roadmap.

    Thanks for the feedback! I
     

    Attached Files:

  20. KatherineHunter

    KatherineHunter

    Joined:
    Jun 8, 2021
    Posts:
    1
    I also used unity 2020 before.1.4, and I can say that there were no problems with this. Given that unity has quite a lot of open-source components, it was not difficult for me to change some of the libraries in order to make it easier for me to work. I added some fonts from UpFonts and just changed the path to them in the code. For me personally, it helps to perceive information a little easier since fonts are the primary transmitter of information in this area. The post above describes everything quite well, so it probably makes no sense to repeat what he said.
     
    Last edited: Jun 11, 2021
  21. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    I couldn't believe it, but just like installing com.unity.ui to make the Runtime UITK work, installing package com.unity.ui.builder directly from Git URL did work.

    I can now see a new field Font Asset on the UI Builder view. I can set it, although due to the mentioned bug, I cannot clear the field back. It causes the error:

    > UI Builder: Font and FontAsset cannot both be set to none.

    But in practice, you can clear neither the Font nor Font Asset. And since the Font Asset has priority, better don't pick the wrong asset if you needed a pure font reference! Worst case, you can completely switch to font assets, or open the UI Document / Style as text file and clear the fields manually...

    Anyway, here is the result. I started with Sample Point Size 90:

    Without RASTER_HINTED font asset
    pasted_image001.png

    With RASTER_HINTED font asset
    pasted_image002.png

    Without RASTER_HINTED font asset (Game view)
    pasted_image003.png

    With RASTER_HINTED font asset (Game view)
    pasted_image004.png

    I still noticed blurry edges and roundy corners though, so I increased the Sample Point Size to make it even crispier:

    With RASTER_HINTED font asset with increased Sample Point Size -> 1200 (Game view)
    pasted_image004.png

    The title logo is very big though and increasing the Sample Point Size requires increasing the Atlas Size to avoid losing characters, it gets crazy quickly. But for smaller button text I found that Sample Point Size of 400 started showing crisp pixel art (no curvy corners, few character strokes of irregular width, no alpha smoothing on character edges).

    It would be even better to have a shader guaranteed to show pixel art text as pixel perfect. Maybe there's a way to literally scale the text... Unity GUI can do that, you just set the scale of the Text object on the Transform, and pixel art is also preserved (to be more exact it seems to re-render the text at dynamic size, since a smooth font will remain smooth). In the UI Builder, you cannot really do that.

    I use a lowres texture to rasterize the entire game so I may also use that at some point, but it's a little risky (the text be aligned perfectly on the screen to avoid bluriness). For now I'll find a compromise between clear characters, keeping pixel art text small and having a Font Atlas that's not too big.
     

    Attached Files:

    HugoBD-Unity likes this.
  22. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    Small update: I realized I could also select my Font Asset > open child assets > Select Atlas > in Inspector, set Filter Mode to Point. It helps a little getting crispier results.

    Unfortunately it doesn't help too much with my new use case which is not even using UITK, just TMP text, but I need to use a dynamic font to support Text Material Outline and so I fall on the same issues (except my text is smaller and for some reason blurry even with Filter Point).

    That may deserve another post for generic TMP text though: how to properly outline pixel art text...
     
  23. sebastian_PE

    sebastian_PE

    Joined:
    Jul 22, 2014
    Posts:
    7
    Hi there
    It seems that the latest version does not recognise .ttf fonts when targeting it inline. At least, nothing happens whne I select it.
     
  24. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @sebastian_PE !

    Could you be more specific on what you are trying to achieve and share a code sample?
     
  25. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    I see the following fixes on Unity 2021.2.0:
    https://unity3d.com/unity/whats-new/2021.2.0

    I have downloaded 2021.2.6f1 so I'll have a look and see what improvements we've got, esp. with Runtime UI Toolkit getting out of preview.