ValueTracker<TValue> Class

Description

Utility element that listens for changes in a SerializedProperty.

public class ValueTracker<TValue> : BindableElement, IEventHandler, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IResolvedStyle, IBindable, INotifyValueChanged<TValue>
Type Parameters
TValue

The type of the property, it doesn't seem to work if it isn't one mentioned in SerializedPropertyType

Inheritance
Remarks

It needs to be added to a panel and bound to work. Many of the uses for this element are covered in Unity 2021 by the TrackPropertyValue and TrackSerializedObjectValue extension methods.

Examples

class ACustomEditor : Editor
{
    public override VisualElement CreateInspectorGUI()
    {
        var root = new VisualElement();
        var intTracker = new ValueTracker<int>();
        root.Add(intTracker);

        // You can pass a property path relative to the object that will be bound.
        intTracker.SetUp("intProperty", e => Debug.Log($"new value: {e.newValue}"));

        // You can pass a serialized property instead of the property path:
        var intProp = serializedObject.FindProperty("intProperty");
        intTracker.SetUp(intProp, e => Debug.Log($"value changed to {e.newValue}"));

        // An optional third value argument sets the initial value of the tracker, 
        // this is to avoid receiving a callback when the tracker is bound.
        intTracker.SetUp(
            intProp,
            e => Debug.Log($"value changed to {e.newValue}"),
            intProp.intValue);

        // You can set up all this from the constructor:
        var intTracker2 = new ValueTracker<int>(
            intProp,
            e => Debug.Log($"new value: {e.newValue}"),
            intProp.intValue);
        root.Add(intTracker2);

        return root;

        // Remember that if we are not inside an inspector, or if we are not tracking
        // a property of the editor's target, we have to bind it manually:
        // root.Bind(serializedObject);
    }
}

Constructors

ValueTracker()

Constructor.

ValueTracker(String, EventCallback<ChangeEvent<TValue>>, TValue)

Convenience constructor that setups the ValueTracker.

ValueTracker(SerializedProperty, EventCallback<ChangeEvent<TValue>>, TValue)

Convenience constructor that setups the ValueTracker.

Fields

ussClassName

USS class name of elements of this type.

Properties

value

The value of the tracker updated by Unity with the property's value.

valueChangedCallback

Delegate called when value changes.

Methods

SetUp(String, EventCallback<ChangeEvent<TValue>>, TValue)

Sets the bindingPath of the tracker, registers a callback and sets an initial value.

SetUp(SerializedProperty, EventCallback<ChangeEvent<TValue>>, TValue)

Sets the bindingPath of the tracker, registers a callback and sets an initial value.

SetValueWithoutNotify(TValue)

Set the value of the tracker without updating the property or triggering callbacks.

Extension Methods

UIToolkitExtensions.GetLocalRect(VisualElement)
UIToolkitExtensions.GetChildren<TElement>(VisualElement, List<TElement>, Func<TElement, Boolean>)
UIToolkitExtensions.GetFirstChild<TElement>(VisualElement, Func<TElement, Boolean>)
UIToolkitExtensions.ForEachChild<TElement>(VisualElement, Action<TElement>)
UIToolkitExtensions.GetBoundSerializedProperty(IBindable)