Version: 2023.2+
This example demonstrates how to use custom USS variables in a custom control.
This example creates a custom control that reads two colors from USS and uses them to generate a texture.
You can find the completed files that this example creates in this GitHub repository.
This guide is for developers familiar with the Unity Editor, UI Toolkit, and C# scripting. Before you start, get familiar with the following:
Create a C# script to define the custom control and a USS file to define the custom style.
create-custom-style-custom-control
to store your files.ExampleElementCustomStyle
folder, create a C# script named ExampleElementCustomStyle.cs
and replace its contents with the following:Create a USS file named ExampleElementCustomStyle.uss
and replace its contents with the following:
Create a UI Document to use the custom control and apply the custom style to the custom control.
ExampleElementCustomStyle
folder, create a UI Document named ExampleElementCustomStyle.uxml
.ExampleElementCustomStyle.uxml
to open it in the UI Builder.ExampleElementCustomStyle.uss
file as an existing USS. This applies the custom USS variables to the square.