Search Unity

Question UI Toolkit doesnt load font via uss

Discussion in 'UI Toolkit' started by Namysoft, Jan 30, 2023.

  1. Namysoft

    Namysoft

    Joined:
    Sep 8, 2018
    Posts:
    10
    Hey guys,

    i build my ui with toolkit and want to change the font of some labels.
    I did it with the following code, but nothing changed in game:

    Code (USS):
    1. .cov-name-label {
    2.     color: #508823;
    3.     font-size: 21px;
    4.     -unity-font: url("/Assets/Resources/Fonts/CambriaMathSDF.asset");
    5. }
    If i try to set the font via UI Builder, everything works fine (screenshot, label on bottom)
    If i try to set the font via code with USS, nothing happens (screenshot, right top corner > character list > Halvar, Ivar, Kjell .....)

    At first i thought there is a path problem, but if i try to change the font path, i get the warnings by unity.
    The selector also works fine, because if i change the color or font size, every change apllies perfectly.

    Unity Version: 2021.3.16f1
     

    Attached Files:

  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Hi,

    From what you describe it looks like something else is overriding the font of the labels. One possibility is that the font is set elsewhere in Uxml or from code. In these cases the styles are considered inline and will have priority over USS.
    I suggest you take a look at the labels with the UI Toolkit debugger to see where the value is coming from.

    Another option is that you have a font asset that take precedence over the font as stated in the documentation.
     
    Last edited: Jan 30, 2023
  3. WongKit

    WongKit

    Joined:
    Apr 27, 2017
    Posts:
    26
    Aren't font assets defined with -unity-font-definition ?
     
  4. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Yes font asset are defined with -unity-font-definition and they take over -unity-font.