StateView<T>
Derive from this class to add custom UI to the view generated for a specific StateView<T>.State type.
Read time 2 minutesLast updated 6 days ago
Definition
- Type: Class
- Namespace: Unity.GraphToolkit.Editor
- Assembly: UnityEditor
public class StateView<T> where T : State
Remarks
A VisualElement is built for every StateView<T>.State that appears in a state machine. A StateView<T> lets you inject custom UI into that generated element. Subclasses are discovered by their type parameter: for a StateView<T>.State of type , the concrete StateView<T> whose type argument matches is used, walking up the StateView<T>.State inheritance chain if there is no exact match.
TThe StateView<T>.State instance is available through StateView<T>.State once the view is constructed. Access the generated visual element through StateView<T>.View and add custom UI to IStateView.Root.
Important: allocate custom UI in StateView<T>.OnViewBuilt and re-add it in StateView<T>.OnCullingChanged (when is ). Do not allocate UI in StateView<T>.OnViewAttached: that callback can fire multiple times during a state's lifetime — for example when the user tabs away from and back to the state machine view — and any UI you allocate there accumulates as duplicates. When a state returns from being culled, the contents of IStateView.Root are cleared, so a cached reference is no longer parented; re-add it from StateView<T>.OnCullingChanged to avoid allocating a new element.
cullingEnabledfalseExamples
// The State type that this view customizes.class MyState : State{}// This view is constructed for every MyState instance in a state machine.class MyStateView : StateView<MyState>{ Label m_Label; public override void OnViewBuilt() { // Allocate custom UI once, after the built-in UI is ready and before the state attaches to a // panel. Cache the reference so OnCullingChanged can re-add it without allocating again. m_Label = new Label($"State: {State.Title}"); View.Root.Add(m_Label); } public override void OnCullingChanged(bool cullingEnabled) { // When cullingEnabled is false, the state has just returned from being culled and Root has // been cleared. Re-add the cached element — no allocation needed. if (!cullingEnabled && m_Label != null) View.Root.Add(m_Label); }}
Properties
Property | Description |
|---|---|
| State | The StateView<T>.State instance this view customizes. |
| View | The generated view for this state. Add custom UI to IStateView.Root. |
Methods
Method | Description |
|---|---|
| OnCullingChanged | Called when the state's culling state changes. |
| OnViewAttached | Called when the state's IStateView.Root is attached to a UI panel. |
| OnViewBuilt | Called once, after the state's built-in UI is fully constructed and before its VisualElement is attached to the state machine view. |
| OnViewDetached | Called when the state's IStateView.Root is detached from its UI panel. |
| OnViewLODChanged | Called when the state machine view's zoom level changes. |