Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to use the <style>-element in UXML to reference a USS-stylesheet

Discussion in 'UI Toolkit' started by tmhbrts, Oct 28, 2019.

  1. tmhbrts

    tmhbrts

    Joined:
    Dec 24, 2013
    Posts:
    13
    Hi all,

    I am currently making a toolbar in UIElements. I based my work on the [UIElements: First Steps] tutorial.
    Using rootVisualElement.styleSheets.Add(), I can apply styles from a USS file to the root visual element:
    Code (CSharp):
    1.  
    2. private void OnEnable()
    3. {
    4.     VisualElement root = rootVisualElement;
    5.     root.styleSheets.Add(Resources.Load<StyleSheet>("ToolbarStyle"));
    6.     VisualTreeAsset toolbarVisualTree = Resources.Load<VisualTreeAsset>("Toolbar");
    7.     toolbarVisualTree.CloneTree(root);
    8.  
    9.     var toolButtons = root.Query<Button>();
    10.     toolButtons.ForEach(SetupButton);
    11. }
    12.  
    Rather, I would prefer to use a <style> element inside the Toolbar.uxml file to set the style, as explained [here] (see: Adding styles to UXML).

    My Toolbar.uxml:
    Code (UXML):
    1. <UXML xmlns="UnityEngine.UIElements">
    2.   <Template path="Assets/Editor/Resources/ButtonTemplate.uxml" name="button-template" />
    3.   <VisualElement class="toolbar">
    4.     <Style src="ToolbarStyle.uss" />
    5.     <VisualElement class="buttons-container">
    6.       <Instance template="button-template" name="cube" />
    7.       <Instance template="button-template" name="sphere" />
    8.       <Instance template="button-template" name="capsule" />
    9.       <Instance template="button-template" name="cylinder" />
    10.       <Instance template="button-template" name="plane" />
    11.     </VisualElement>
    12.   </VisualElement>
    13. </UXML>
    14.  
    My ToolBarStyle.uss: (in the same folder as Toolbar.uxml
    Code (USS):
    1. .buttons-container {
    2.     flex-direction: row;
    3.     flex-wrap: wrap;
    4. }
    5.  
    6. .toolbar-button {
    7.     width: 30px;
    8.     height: 30px;
    9.     align-items: flex-start;
    10.     justify-content: center;
    11. }
    12.  
    13. .toolbar-button-icon {
    14.     height: 20px;
    15.     width: 20px;
    16. }
    With this setup, I only get the style from the uss file applied using root.styleSheets.Add(). When I comment out this part from the c# code and use the script element in UXML instead, it does not work. The console shows this warning:

    "Assets/Editor/Resources/Toolbar.uxml (4,6): Semantic - USS in 'style' attribute is invalid: <null>"

    How should I use the style element in UXML?
     
    Last edited: Oct 28, 2019
  2. wbahnassi_unity

    wbahnassi_unity

    Unity Technologies

    Joined:
    Mar 12, 2018
    Posts:
    28
    Hi, I tried applying the same tutorial and changed the USS to be specified in UXML instead of CSS, and all worked great. The error message you have mentions 'style` attribute. Can you double-check it's <Style ... /> instead of <style ... /> as the latter is not recognized.
     
  3. tmhbrts

    tmhbrts

    Joined:
    Dec 24, 2013
    Posts:
    13
    I looked into this again. It seemed that I was reading the manual for 2020.1. In 2019.2 the style element has to be given a "path" attribute (value: absolute path to uss file from project root) attribute rather than a "src" attribute.
     
    wbahnassi_unity likes this.
  4. wbahnassi_unity

    wbahnassi_unity

    Unity Technologies

    Joined:
    Mar 12, 2018
    Posts:
    28
    Yes, and my test was on 2020.1 too.. so, yeah :rolleyes:
     
  5. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,203
    To confirm, in 2019.3 and newer versions of Unity, the `<Style>` tag should use the `src=` attribute, not the `path=` attribute.
     
  6. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,184
    I kind of have the same question, still. It seems to work loading the style sheet via UXML, but I still get an IDE warning in Visual Studio:

    upload_2020-1-29_18-2-34.png

    The warning says: invalid child element "Style".

    Checked in Unity 2019.3.0f6.
     
    Last edited: Jan 30, 2020
  7. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    386
    This is a bug in the xml schema generation, thanks for reporting.
     
    Xarbrough likes this.
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Was that ever tracked and fixed? I'm seeing the same thing under 2019.4.10

    upload_2020-9-21_22-57-5.png
     
  9. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    386
    This was fixed in 2020.1. Unfortunately, it's highly unlikely this will be backported to 19.4
     
  10. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Fair enough. As long as it's just a harmless warning, that's fine.