Skip to main content

VisualElement Extensions

UI Toolkit interfaces in one call chain — no separate line per property and style.

Quick start​

Before — Unity APIAfter — FastTools
var title = new Label("Ability Config");
title.style.fontSize = 14;

var header = new VisualElement();
header.style.paddingLeft = 12;
header.style.paddingRight = 12;
header.style.paddingTop = 10;
header.style.paddingBottom = 10;
header.Add(title);
var header = new VisualElement()
.SetPaddingX(12)
.SetPaddingY(10)
.AddChild(new Label("Ability Config")
.SetFontSize(14));

The chain keeps the type: new Button().SetText("Create") is a Button, and AddChild returns the parent, not the added child.

Method names​

UnityFastTools
tooltip = "Mana cost"SetTooltip("Mana cost")
isDelayed = trueSetDelayed(true)
style.fontSize = 14SetFontSize(14)
clicked += RefreshAddClicked(Refresh)
bindItem = BindRowSetBindItem(BindRow)
Focus()FocusSelf()
Init(theme)Initialize(theme)

Other Unity methods get names by meaning — see the sections below. The full list is in the API reference.

Children​

UnityFastTools
AddAddChild, AddChildren
InsertInsertChild, InsertChildren
RemoveRemoveChild, RemoveChildren
RemoveAtRemoveChildAt
ClearClearChildren
Before — Unity APIAfter — FastTools
if (isFree)
body.Add(helpBox);
body.AddChildIf(isFree, helpBox);

Every method in the first table has an …If variant.

Styles​

Before — Unity APIAfter — FastTools
header.style.marginTop = 4;
header.style.marginBottom = 4;
header.style.borderBottomWidth = 1;
header
.SetMarginY(4)
.SetBorderWidth(bottom: 1);
every side

The other style methods work the same way.

Colors from strings and assets from Resources​

Before — Unity APIAfter — FastTools
if (ColorUtility.TryParseHtmlString(
"#FFC24D", out var color))
badge.style.color = color;
badge.SetColor("#FFC24D");
root.style.backgroundImage = Resources
.Load<Texture2D>("UI/Card");
root.SetBackgroundImageFromResources(
"UI/Card");

SetImage, SetSprite, SetVectorImage, AddStyleSheet and RemoveStyleSheet also have a …FromResources variant.

Bold and italic​

SetNormalUnityFontStyleAndWeight() resets both flags; the other presets change one flag and keep the other:

MethodChanges
AddBold…Normal → Bold, Italic → BoldAndItalic
RemoveBold…Bold → Normal, BoldAndItalic → Italic
AddItalic…Normal → Italic, Bold → BoldAndItalic
RemoveItalic…Italic → Normal, BoldAndItalic → Bold
note

The presets read the current value from the element's style, not the resolved style: a weight set in USS counts as Normal.

USS classes and style sheets​

UnityFastTools
AddToClassListAddClass
RemoveFromClassListRemoveClass
ToggleInClassListToggleClass
EnableInClassListEnableClass
ClearClassListClearClasses
styleSheets.AddAddStyleSheet
styleSheets.RemoveRemoveStyleSheet

Values and events​

UnityFastTools
value = 10SetValue(10)
SetValueWithoutNotify(10)SetValue(10, notify: false)
RegisterValueChangedCallbackAddValueChanged
UnregisterValueChangedCallbackRemoveValueChanged
  • AddValueChanged(evt => …) needs no type arguments: overloads cover every Unity field and, with com.unity.mathematics installed, its types;
  • for your own types — SetValue<T, TValue>(…) and AddValueChanged<TField, TValue>(…).

Focus​

Before — Unity APIAfter — FastTools
if (manaCost.focusController?
.focusedElement == manaCost)
Refresh();
if (manaCost.IsFocused())
Refresh();

Manipulators​

Before — Unity APIAfter — FastTools
title.AddManipulator(
new Clickable(Refresh));
title.AddClickable(Refresh);
title.AddManipulator(
new KeyboardNavigationManipulator(
OnNavigate));
title.AddKeyboardNavigationManipulator(
OnNavigate);
title.AddManipulator(
new ContextualMenuManipulator(
BuildMenu));
title.AddContextualMenuManipulator(
BuildMenu);
var clickable = new Clickable(Refresh);
title.AddManipulator(clickable);
title.RemoveManipulator(clickable);
title.AddClickable(
Refresh, out var clickable);
title.RemoveManipulatorSelf(clickable);

Editor extensions​

Before — Unity APIAfter — FastTools
manaCost.bindingPath = "_manaCost";
manaCost.Bind(serializedObject);
manaCost.BindTo(
serializedObject, "_manaCost");
manaCost.bindingPath = "_manaCost";
manaCost.SetBindingPath(
"_manaCost");
manaCost.BindProperty(property);
manaCost.BindPropertyTo(property);
manaCost.Unbind();
manaCost.UnbindFrom();

PropertyField gets SetLabel and AddValueChanged / RemoveValueChanged with a SerializedPropertyChangeEvent:

var manaCost = new PropertyField(
serializedObject.FindProperty("_manaCost"))
.SetLabel("Mana cost")
.AddValueChanged(_ => Refresh());

To write a property from your own code, see SerializedProperty Extensions.

Open the script on double-click​

AddOpenScriptCommand opens the script of a MonoBehaviour or ScriptableObject in the IDE on a left double-click; for any other object it adds nothing:

title.AddOpenScriptCommand(target);

The element's window​

Before — Unity APIAfter — FastTools
var panel = title.panel;
var window = Resources
.FindObjectsOfTypeAll<EditorWindow>()
.FirstOrDefault(w => panel ==
w.rootVisualElement.panel);
if (!window)
window = EditorWindow.focusedWindow;
if (!window)
window = EditorWindow.mouseOverWindow;
var window = title.GetOwnerWindow();

Custom USS properties​

Before — Unity APIAfter — FastTools
if (evt.customStyle.TryGetValue(
ThemeProperty, out var raw)
&& Enum.TryParse(raw,
ignoreCase: true,
out PreviewTheme theme))
ApplyTheme(theme);
if (evt.customStyle.TryGetByEnum(
ThemeProperty,
out PreviewTheme theme))
ApplyTheme(theme);

Package sample​

The catalog window and the AbilityConfig inspector in the EditorTools sample are built with these extensions.

The Ability Catalog window from the EditorTools sampleThe Ability Catalog window from the EditorTools sample