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:
private void OnEnable()
{
VisualElement root = rootVisualElement;
root.styleSheets.Add(Resources.Load<StyleSheet>("ToolbarStyle"));
VisualTreeAsset toolbarVisualTree = Resources.Load<VisualTreeAsset>("Toolbar");
toolbarVisualTree.CloneTree(root);
var toolButtons = root.Query<Button>();
toolButtons.ForEach(SetupButton);
}
Rather, I would prefer to use a element inside the Toolbar.uxml file to set the style, as explained [here] (see: Adding styles to UXML).
My Toolbar.uxml:
<UXML xmlns="UnityEngine.UIElements">
<Template path="Assets/Editor/Resources/ButtonTemplate.uxml" name="button-template" />
<VisualElement class="toolbar">
<Style src="ToolbarStyle.uss" />
<VisualElement class="buttons-container">
<Instance template="button-template" name="cube" />
<Instance template="button-template" name="sphere" />
<Instance template="button-template" name="capsule" />
<Instance template="button-template" name="cylinder" />
<Instance template="button-template" name="plane" />
</VisualElement>
</VisualElement>
</UXML>
My ToolBarStyle.uss: (in the same folder as Toolbar.uxml
.buttons-container {
flex-direction: row;
flex-wrap: wrap;
}
.toolbar-button {
width: 30px;
height: 30px;
align-items: flex-start;
justify-content: center;
}
.toolbar-button-icon {
height: 20px;
width: 20px;
}
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: ”
How should I use the style element in UXML?