Theme API
Video tutorial: Themes. All videos: Video tutorials.
| Member | Kind | Effect |
|---|---|---|
theme-name |
UXML attribute (ThemeName) |
The theme to load on attach. A new value on an attached workspace switches the theme, as SetTheme does. Default: default. |
auto-load-styles |
UXML attribute (AutoLoadStyles) |
If false, WindowStyles.uss and the theme sheet are not loaded. Default: true. |
SetTheme(string themeName) |
Method | Removes the previous theme sheet and adds UIToolbox/Themes/{themeName}. User sheets stay above it. default or dark removes the theme sheet. It also swaps the theme class, such as tb-theme--game, on the workspace. |
ThemeChanged |
Event | Raised after SetTheme switches the theme, with the new theme name. The new sheet and the new class are on the workspace when it is raised. The same theme raises nothing. |
ThemeClass(string? themeName) |
Static method | The class of a theme on its workspace: tb-theme-- and the theme name. null, "", default and dark give tb-theme--dark. See The theme class. |
THEMES_FOLDER |
Constant | "UIToolbox/Themes/", the Resources folder of the theme sheets. Resources.Load<StyleSheet>(Workspace.THEMES_FOLDER + "game") loads a theme sheet for an element that is not a workspace. |
AvailableThemes |
Static property | The names of the sheets in Resources/UIToolbox/Themes: dark first, then the others by name. A theme of your game is in the list when its sheet is in a Resources/UIToolbox/Themes folder. |
AddUserStyleSheet(StyleSheet) |
Method | Adds a sheet above the theme. Theme changes do not remove it. |
RemoveUserStyleSheet(StyleSheet) |
Method | Removes a sheet that you added with AddUserStyleSheet. |
text-size |
UXML attribute (TextSize) |
Text size: Normal, Large (125%), Larger (150%) or Largest (200%). You can change it at any time. Default: Normal. |
You can call these methods before the workspace attaches. They apply on attach. ThemeSelector is a dropdown of the themes.
Built-in themes
| Theme | Look |
|---|---|
dark |
The base palette, VS Code dark. default is the same. |
light |
Light gray surfaces and dark text. |
high-contrast |
Black, white and yellow, with thicker borders. |
toolbox |
The UI Toolbox logo colors: charcoal surfaces, a blue accent and green for success and dock targets. |
toolbox-light |
The logo colors on light gray. |
game |
Night glass windows with a blur, a gold accent, the Rajdhani font and glossy buttons. See The Game theme. |
game-custom |
An example user sheet with a purple palette and larger sizes. It also works as a theme. |
dark stays the default of the package and of the samples. Every other theme is opt-in.
The Game theme
SetTheme("game") gives the workspace the look of a game menu. It shows that one style sheet can change every part of the toolbox.
- Windows and dialogs are frosted glass: a dark see-through fill with a blur of what is behind them, a soft shadow and round corners. Each blur is a render pass, so only windows and dialogs blur.
- The accent is gold (
#ffb43a). Titles and buttons use Rajdhani Bold (Runtime/Resources/UIToolbox/Fonts). - Action buttons have a gloss image (
Themes/game-gloss.png), turn gold on hover, grow a little on hover and shrink a little on press. The primary variant is gold and the danger variant is red. - Chrome buttons stay flat: the window buttons, the tab close, the close buttons of the overlays, the clear buttons, the arrows, the calendar arrows, the keys of the on-screen keyboard, the code block copy and the like. They have no gloss and no scale.
- The workspace background is an opaque night blue (
#0b0a1a). WithBackgroundOpacitybelow 1, a scene behind it shows through. - At the
Normaltext size, buttons and their labels are a little larger than indark. The other text sizes set their own sizes.
Your own Button in a workspace with the Game theme gets the gloss too. To keep one flat, turn the gloss off for the Game theme only:
.tb-theme--game .my-flat-button {
background-image: none;
}
The theme class
Each workspace has the class of its theme: tb-theme--dark for the base palette, tb-theme--light, tb-theme--game and so on (Workspace.ThemeClass(name)). SetTheme swaps it before it raises ThemeChanged. With the class, a sheet can style a part for one theme only:
.tb-theme--game .my-hud {
border-color: var(--tb-color-accent);
}
The class is on the workspace only, so an element outside the workspace does not have it. The workspace gives its open dropdown menus its theme sheet, and the :root rule of the sheet matches the menu. To give another element outside the workspace a theme, add the theme sheet to it: Resources.Load<StyleSheet>(Workspace.THEMES_FOLDER + "game").
Text size
TextSize is for users who need larger text. Any value other than Normal adds the class tb-text-size--125, --150 or --200 (Workspace.TextSizeClass) to the workspace and to its open dropdown menus. The rules for these classes in WindowStyles.uss set fixed values for the font size variables, --tb-size-title-bar and --tb-size-button.
- Unity's controls in the workspace inherit
--tb-font-sizefrom the workspace, so they also scale. - Icon glyphs (rating stars, FlipView arrows, NumericUpDown arrows) keep their size.
- A theme's own font and bar sizes apply at
Normalonly. USS has nocalc(), so a step cannot multiply the theme values. - A theme switch keeps the text size. For a high-contrast, large-text setup, use
SetTheme("high-contrast")andTextSize = TextSize.Larger.
<tb:Workspace theme-name="high-contrast" text-size="Larger" />
Set the theme
In UXML:
<tb:Workspace name="workspace" theme-name="light" />
In code:
#nullable enable
using UIToolbox;
using UnityEngine;
using UnityEngine.UIElements;
public class ThemeSwitcher : MonoBehaviour
{
[SerializeField] private StyleSheet? _brandSheet;
private void Start()
{
var workspace = GetComponent<UIDocument>().rootVisualElement
.Q<Workspace>();
workspace.SetTheme("high-contrast");
if (_brandSheet != null)
workspace.AddUserStyleSheet(_brandSheet);
}
}