UI Toolbox logoUI Toolbox
Support

Toolbox controls in the Inspector

The value controls of the toolbox derive from BaseField<T>, so they work in the Unity Inspector too. Bind a control to a serialized field of the same type, and the Inspector fills it and writes each edit back, with undo, to each selected object.

There are two ways:

The example scripts are in Assets/UIToolbox/Examples/InspectorFields (Jump, JumpPad, ToolboxCurveAttribute) and Assets/UIToolbox/Examples/Editor (ToolboxCurveDrawer, JumpPadEditor).

Try the example

  1. Click GameObject > 3D Object > Cube.
  2. In the Inspector, click Add Component and add Jump and Jump Pad.
  3. The Height of Jump is a toolbox CurveField. Click the curve to open the curve editor.
  4. Jump Pad shows a Knob, a RangeSlider, a ColorPicker swatch and a GradientField. Click the swatch or the gradient to open its editor.
  5. Enter Play mode, right-click the header of Jump and click Play the jump.

Way 1: an attribute and a property drawer

The attribute goes in a runtime script, so a component can use it:

public class ToolboxCurveAttribute : PropertyAttribute { }

public class Jump : MonoBehaviour
{
    [ToolboxCurve] public AnimationCurve height;
}

The drawer goes in an Editor folder or an editor assembly. CreatePropertyGUI returns the control, and BindProperty binds it to the field:

[CustomPropertyDrawer(typeof(ToolboxCurveAttribute))]
public class ToolboxCurveDrawer : PropertyDrawer
{
    public override VisualElement CreatePropertyGUI(
        SerializedProperty property)
    {
        var field = new UIToolbox.CurveField(property.displayName);
        field.AddToClassList(
            BaseField<AnimationCurve>.alignedFieldUssClassName);
        field.BindProperty(property);
        return field;
    }
}

Way 2: a custom Editor

CreateInspectorGUI builds the Inspector. Each control names its field in bindingPath. The Inspector binds the returned element to the selected objects, so the controls need no BindProperty call:

[CustomEditor(typeof(JumpPad)), CanEditMultipleObjects]
public class JumpPadEditor : Editor
{
    public override VisualElement CreateInspectorGUI()
    {
        var root = new VisualElement();
        root.Add(new Knob("Launch speed")
        {
            bindingPath = nameof(JumpPad.launchSpeed),
            MaxValue = 30,
            UseWheel = false,
        });
        return root;
    }
}

Which control for which field

Field type Toolbox controls
float Knob, Rating, NumericUpDown
double NumericUpDown
int OptionSelector
Vector2 RangeSlider (a low and a high value), XYPad (a point)
Color ColorPicker
Gradient GradientField
AnimationCurve CurveField
string ComboBox, SearchDropdown, CodeInput, KeyBindingField

Values that Unity does not serialize

Unity does not serialize DateTime or TimeSpan, and the list value of a TagField does not bind to a serialized array. So DatePicker, TimePicker, TimeSpanPicker and TagField cannot bind to a field directly. Keep the value in a type that Unity serializes, and convert it in the drawer:

In the drawer, set the control value from the property with SetValueWithoutNotify. In RegisterValueChangedCallback, write the converted value to the property and call serializedObject.ApplyModifiedProperties(), which records the undo. TrackPropertyValue on the control calls you back when the property changes elsewhere, such as after an undo.

Names that Unity has too

UnityEditor.UIElements has a CurveField and a GradientField of its own. In a file with using UnityEditor.UIElements;, write UIToolbox.CurveField, or add an alias such as using CurveField = UIToolbox.CurveField;.

Style sheets and popups

A toolbox control loads its style sheets when it attaches to a panel. In the editor, it also loads them on the editor window and on the Inspector, so the controls and their popups, such as the curve editor and the color picker, keep the toolbox look. See Sheet load order.

Next steps