This module is part of the Unity Essentials ecosystem and follows the same lightweight, editor-first approach. Unity Essentials is a lightweight, modular set of editor utilities and helpers that streamline Unity development. It focuses on clean, dependency-free tools that work well together.
All utilities are under the UnityEssentials namespace.
using UnityEssentials;
Install the Unity Essentials entry package via Unity's Package Manager, then install modules from the Tools menu.
-
Add the entry package (via Git URL)
- Window → Package Manager
- "+" → "Add package from git URL…"
- Paste:
https://github.com/EggyStudio/UnityEssentials.git
-
Install or update Unity Essentials packages
- Tools → Install & Update UnityEssentials
- Install all or select individual modules; run again anytime to update
Quick overview: Drop-in MonoBehaviours for common UI Toolkit tasks - event routing, OnValueChanged handlers, label/background setters, and display toggling - powered by the UI Element Linker. No custom editor code required.
This module bundles small, focused behaviours that work with the Element Linker to control and listen to UI Toolkit elements without boilerplate. Add them to a GameObject alongside a UIElementLink or UIElementQuery to forward click/key/pointer/focus events, react to value changes in common controls, set labels and backgrounds, and toggle display - all in Edit or Play Mode and across one or many linked elements.
- UI Element linking (via Element Linker)
- Bind one or many VisualElements from a UIDocument using
UIElementLink(single) orUIElementQuery(query by name/class/type) - Components work on the linked set; no manual
root.Q()in your scripts
- Bind one or many VisualElements from a UIDocument using
- Components included
- UI Event Handler (
UIEventHandler)- UnityEvents for: Click, Key (Down/Up with
KeyCode), Pointer (Down/Up/Enter/Leave withVector3), Focus/Blur - Also exposes typed event payloads (
ClickEvent,KeyDownEvent, etc.) if you need the full UI Toolkit event - Registers only the callbacks you’ve assigned → minimal overhead; unregisters on disable/destroy
- UnityEvents for: Click, Key (Down/Up with
- UI On Value Changed Handler (
UIOnValueChangedEventHandler)- Routes
ChangeEvent<T>for supported elements:TextField,Label,Slider,SliderInt,DropdownField,Foldout - Two UnityEvents per type: one with the new value (e.g.,
float,string,bool) and one with the fullChangeEvent<T> - Clean inspector using
[ShowIf]to show only relevant sections per linked element type
- Routes
- UI Set Label (
UISetLabel)- Sets the
Label.textof all linkedLabelelements - Simple
LabelTextproperty updates the UI immediately
- Sets the
- UI Set Background (
UISetBackground)- Sets background color and/or texture on all linked elements (uses UI Toolkit Extensions)
- UI Display Toggle (
UIDisplayToggle)- Shows/hides linked elements using
display: flex/none - Optional "Display On Start" and a public
Toggle()method
- Shows/hides linked elements using
- UI Event Handler (
- Works in Edit Mode (where applicable)
UIEventHandlerandUIOnValueChangedEventHandlerare[ExecuteAlways]so you can test from the editor
- Unity Editor 6000.0+ (Editor/Runtime UI Toolkit)
- A
UIDocumentin your scene - Dependencies (Unity Essentials modules)
- UI Toolkit Element Linker: provides
UIElementLink,UIElementQuery, andUIScriptComponentBase - UI Toolkit Extensions: color/image helpers like
SetBackgroundImage,SetBackgroundColor - Editor Attributes – ShowIf: attributes used by the value-changed handler’s inspector (
[ShowIf],[ShowIfNot],[Info])
- UI Toolkit Element Linker: provides
Tip: If a component seems inactive, make sure the GameObject also has either a UIElementLink or UIElementQuery set up and points at existing elements in the UIDocument.
- Link elements
- Add
UIElementLinkto bind a single VisualElement (drag from a UI Builder-referenced UIDocument or query in code) - Or add
UIElementQueryto bind multiple elements by name/class/type - Keep the linker and the component on the same GameObject
- Add a component
- Choose one or more of:
UIEventHandlerUIOnValueChangedEventHandlerUISetLabelUISetBackgroundUIDisplayToggle
- Configure and wire events
- For event components, expand the relevant UnityEvent and hook your methods
- For setters, edit
LabelText,BackgroundColor,BackgroundTexture, or "Display On Start"
Examples
- Button click
// On a GameObject with UIElementLink -> (your Button) // Add UIEventHandler and wire in Inspector, or do it from code: public class ButtonActions : MonoBehaviour { public void OnClicked() => Debug.Log("Button clicked"); }
- Slider value changed
// On a GameObject with UIElementQuery -> (your Slider) // Add UIOnValueChangedEventHandler; in the Inspector, wire SliderEvents.OnSliderChanged(float) public class SliderActions : MonoBehaviour { public void OnValue(float v) => Debug.Log($"Slider = {v}"); }
- Set label text from code
var labelSetter = GetComponent<UISetLabel>(); labelSetter.LabelText = "Hello UI Toolkit";
- Toggle display at runtime
GetComponent<UIDisplayToggle>().Toggle();
- Linking and base class
UIScriptComponentBaselooks forUIElementLinkorUIElementQueryon the same GameObject and caches:Document(theUIDocument),LinkedElements(array), andType(UIElementType)
- Helper
IterateLinkedElements(...)is used by all components to apply changes to every linked element
- Event components
- Register callbacks on
OnEnableonly for events that have listeners; unregister onOnDisable/OnDestroy - UI Toolkit events are forwarded both as simplified values and as full event structs
- Register callbacks on
- Setters
UISetLabelupdatesLabel.textonStartand wheneverLabelTextchangesUISetBackgrounduses extension methods to apply color/texture onOnEnable
- Display toggle
UIDisplayToggleflipsstyle.displaybetweenFlexandNone; providesSetEnabled(bool)andToggle()
- Supported value-changed elements:
TextField,Label,Slider,SliderInt,DropdownField,Foldout- Other controls aren’t wired by default; extend
UIOnValueChangedEventHandlerif needed
- Other controls aren’t wired by default; extend
- Requires a valid link
- If
LinkedElementsis empty or the query doesn’t match, components do nothing safely
- If
- Edit Mode behavior
UIEventHandlerandUIOnValueChangedEventHandlerare[ExecuteAlways]; avoid heavy listeners that should only run in Play Mode
- Background texture/color
- If
BackgroundTextureis null, only color is applied
- If
- UnityEvents
- Invoked on the main thread; ensure subscribers are lightweight to avoid frame hitches
Runtime/UIEventHandler.cs– Click/Key/Pointer/Focus event routingRuntime/UIOnValueChangedEventHandler.cs– Value-changed routing for common elementsRuntime/UISetLabel.cs– SetsLabel.textfor linked labelsRuntime/UISetBackground.cs– Sets background color/texture for linked elementsRuntime/UIDisplayToggle.cs– Show/hide (display) toggling with optional start stateRuntime/UnityEssentials.UIToolkitScriptComponents.asmdef– Runtime assembly definition
unity, ui toolkit, visualelement, events, value-changed, label, background, toggle, script components, runtime
