ParagonVisualElement
Abstract base class for reusable Paragon visual elements. Extends Unity's VisualElement with a managed lifecycle (create → initialize → destroy), automatic UXML template loading via [UxmlTemplate], element querying helpers, and guard-based single-creation semantics. The workhorse base for all custom UI elements in the Paragon editor framework.
Definition
Namespace: Paragon.Editor Assembly: Paragon.Editor.dll
public abstract class ParagonVisualElement : VisualElementInheritance: VisualElement → ParagonVisualElement
Remarks
The creation lifecycle is guarded by a created flag — CreateGUI() will only execute once, even if called multiple times or triggered by both the constructor (createImmediately) and the panel attach event. The lifecycle is:
Construction — Registers
AttachToPanelEventandDetachFromPanelEventcallbacks. Optionally callsCreateGUI()immediately.CreateGUI()— Loads the UXML template viaGetTemplateAsset(), clones it, copies style sheets, then callsOnCreateGUI(). SchedulesInitialize()viadelayCall.OnInitialize()— Runs one frame afterOnCreateGUI(). SetsInitializedtotrue.OnDestroy()— Called when the element detaches from its panel.
Quick Lookup
Create an element
ParagonVisualElement.Create<MyElement>() or new MyElement()
Create with immediate GUI
ParagonVisualElement.CreateImmediate<MyElement>() or pass createImmediately: true
Bind a UXML template
Add [UxmlTemplate("TemplateName")] to the class
Query child elements
Q("name") or Q<Button>("name")
Toggle visibility
SetDisplay(true/false)
Check if ready
element.Initialized
Properties
Initialized
bool
public
true after OnInitialize() completes
Constructors
name
string
null
Element name. Defaults to GetType().Name if null
createImmediately
bool
false
If true, calls CreateGUI() in the constructor instead of waiting for panel attach
Methods
OnCreateGUI
Abstract callback invoked after the UXML template is cloned. Override to build the element's visual tree — query template elements, add children, wire event callbacks.
OnInitialize
Virtual callback invoked one frame after OnCreateGUI() via EditorApplication.delayCall.
OnDestroy
Virtual callback invoked when the element detaches from its panel.
Reset
Virtual method for resetting element state. Default implementation is empty.
GetTemplateAsset
Virtual method that resolves the UXML template. Default implementation reads the [UxmlTemplate] attribute from the class via reflection.
SetDisplay
Toggles element visibility by setting display style to Flex or None.
Q
Queries children by name.
Extension Points
Required Overrides
OnCreateGUI()
Build the element's visual tree — query template elements, add children
Optional Overrides
OnInitialize()
Deferred setup one frame after creation
OnDestroy()
Cleanup when element detaches from panel
Reset()
Reset element state (e.g., clear fields)
GetTemplateAsset()
Custom template resolution (default: reads [UxmlTemplate] attribute)
Implementation Requirements
When subclassing, you MUST:
Override
OnCreateGUI()— this is abstractUse a parameterless constructor (required for
Create<T>()factory methods)
You SHOULD:
Add
[UxmlTemplate("TemplateName")]to bind a UXML fileQuery template elements in
OnCreateGUI()usingQ()Use
OnInitialize()for setup that requires the visual tree to be fully attachedUse
SetDisplay()instead of directly manipulatingstyle.display
You MUST NOT:
Query template children before
OnCreateGUI()runs — the template is not yet clonedAssume
InitializedistrueduringOnCreateGUI()— it is set afterOnInitialize()
Static Methods
Create<T>
Factory method that creates an element instance. GUI creation is deferred to panel attachment.
CreateImmediate<T>
Factory method that creates an element and immediately calls CreateGUI().
Common Pitfalls
GUI creation only happens once. The created flag ensures CreateGUI() runs exactly once. If you call CreateImmediate(), the panel attach event will not trigger a second creation. However, OnCreateGUI() runs before the element is attached to any panel when using createImmediately — some layout queries may not work yet.
Template children are moved, not cloned. CloneTemplate() moves children from the template root into this element using Add(). The TemplateContainer wrapper is discarded. Style sheets are explicitly copied from the template to the element.
name defaults to the type name. If you don't pass a name to the constructor or factory, the element's name is set to GetType().Name. This makes Q() queries predictable in parent elements.
Examples
Basic Visual Element with Template
Programmatic Element (No Template)
Usage in a ParagonEditor
See Also
ParagonVisualElement<T> — generic self-referencing variant with simplified factories
ParagonVisualElementDrawer<TProperty> — uses ParagonVisualElement as its inner property element
ParagonEditor — base editor class with similar template lifecycle
UxmlTemplateAttribute — attribute for binding UXML templates
Objects — editor objects overview
Last updated