# StateView<T>

> Derive from this class to add custom UI to the view generated for a specific StateView<T>.State type.

## Definition

* **Type:** Class
* **Namespace:** [Unity.GraphToolkit.Editor](/engine/6000.7/script-reference/unity/graphtoolkit/editor.md)
* **Assembly:** UnityEditor

```csharp
public class StateView<T> where T : State
```

## Remarks

A [VisualElement](/engine/6000.7/script-reference/unityengine/uielements/visualelement.md) is built for every [StateView\<T>.State](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/state.md) that appears in a state machine. A [StateView\<T>](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1.md) lets you inject custom UI into that generated element. Subclasses are discovered by their type parameter: for a [StateView\<T>.State](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/state.md) of type `T`, the concrete [StateView\<T>](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1.md) whose type argument matches is used, walking up the [StateView\<T>.State](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/state.md) inheritance chain if there is no exact match.

The [StateView\<T>.State](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/state.md) instance is available through [StateView\<T>.State](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/state.md) once the view is constructed. Access the generated visual element through [StateView\<T>.View](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/view.md) and add custom UI to [IStateView.Root](/engine/6000.7/script-reference/unity/graphtoolkit/editor/istateview/root.md).

**Important:** allocate custom UI in [StateView\<T>.OnViewBuilt](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/onviewbuilt.md) and re-add it in [StateView\<T>.OnCullingChanged](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/oncullingchanged.md) (when `cullingEnabled` is `false`). Do not allocate UI in [StateView\<T>.OnViewAttached](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/onviewattached.md): 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](/engine/6000.7/script-reference/unity/graphtoolkit/editor/istateview/root.md) are cleared, so a cached reference is no longer parented; re-add it from [StateView\<T>.OnCullingChanged](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/oncullingchanged.md) to avoid allocating a new element.

## Examples

```csharp
// 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](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/state.md) | The [StateView\<T>.State](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/state.md) instance this view customizes.              |
| [View](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/view.md)   | The generated view for this state. Add custom UI to [IStateView.Root](/engine/6000.7/script-reference/unity/graphtoolkit/editor/istateview/root.md). |

## Methods

| Method                                                                                                       | Description                                                                                                                                                                                                    |
| ------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [OnCullingChanged](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/oncullingchanged.md) | Called when the state's culling state changes.                                                                                                                                                                 |
| [OnViewAttached](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/onviewattached.md)     | Called when the state's [IStateView.Root](/engine/6000.7/script-reference/unity/graphtoolkit/editor/istateview/root.md) is attached to a UI panel.                                                             |
| [OnViewBuilt](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/onviewbuilt.md)           | Called once, after the state's built-in UI is fully constructed and before its [VisualElement](/engine/6000.7/script-reference/unityengine/uielements/visualelement.md) is attached to the state machine view. |
| [OnViewDetached](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/onviewdetached.md)     | Called when the state's [IStateView.Root](/engine/6000.7/script-reference/unity/graphtoolkit/editor/istateview/root.md) is detached from its UI panel.                                                         |
| [OnViewLODChanged](/engine/6000.7/script-reference/unity/graphtoolkit/editor/stateview1/onviewlodchanged.md) | Called when the state machine view's zoom level changes.                                                                                                                                                       |
