Class GradientEditor
Unity's gradient stop-bar editor: alpha stops above a live preview strip, color stops below, and an inspector for whichever stop is selected.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
public class GradientEditor : VisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, INotifyValueChanged<Gradient>, ICustomColorGradientFieldPicker
Remarks
The editor is the counterpart ColorGradientField has been waiting for. It implements ICustomColorGradientFieldPicker, so assigning one to customPicker is all it takes to turn the field from a read-only gradient well into an editable one — exactly how ColorField and ColorPicker pair up.
Everything visual is reused rather than reinvented. The preview strip is a ColorSwatch, the same shader path ColorGradientField's own preview uses, so Fixed and the transparency checkerboard are rendered once and identically in both places. The selected color stop is edited with a ColorField, which brings the whole ColorPicker with it, and the selected alpha stop with a ColorSlider whose track is the sampled stop color fading to transparent.
Stops are held sorted by time. Dragging one past a neighbour therefore reorders the list, and the selection follows the stop rather than the index, so a drag never silently starts editing a different stop. Both rails are capped at maxKeys, which is Gradient's own limit: a ninth key would be dropped by SetKeys(GradientColorKey[],GradientAlphaKey[]) without an error.
Interaction
- Click a stop to select it, drag to move it.
- Double-click an empty rail to add a stop there, sampled from the current gradient so inserting one does not change how the gradient looks.
DeleteorBackspaceremoves the selected stop, never the last one on a rail.
Example
var field = new ColorGradientField { customPicker = new GradientEditor() };
field.RegisterValueChangedCallback(evt => Debug.Log(evt.newValue));
Constructors
GradientEditor()
Default constructor. Starts on an opaque black-to-white blend.
Declaration
public GradientEditor()
Fields
alphaRailUssClassName
The alpha stop rail styling class.
Declaration
public const string alphaRailUssClassName = "appui-gradienteditor__alpha-rail"
Field Value
| Type | Description |
|---|---|
| string |
alphaSliderUssClassName
The stop alpha slider styling class.
Declaration
public const string alphaSliderUssClassName = "appui-gradienteditor__alpha-slider"
Field Value
| Type | Description |
|---|---|
| string |
colorFieldUssClassName
The stop color field styling class.
Declaration
public const string colorFieldUssClassName = "appui-gradienteditor__color-field"
Field Value
| Type | Description |
|---|---|
| string |
colorRailUssClassName
The color stop rail styling class.
Declaration
public const string colorRailUssClassName = "appui-gradienteditor__color-rail"
Field Value
| Type | Description |
|---|---|
| string |
hdrUssClassName
The HDR styling class.
Declaration
public const string hdrUssClassName = "appui-gradienteditor--hdr"
Field Value
| Type | Description |
|---|---|
| string |
inspectorUssClassName
The inspector styling class.
Declaration
public const string inspectorUssClassName = "appui-gradienteditor__inspector"
Field Value
| Type | Description |
|---|---|
| string |
labelUssClassName
The inspector label styling class.
Declaration
public const string labelUssClassName = "appui-gradienteditor__label"
Field Value
| Type | Description |
|---|---|
| string |
locationFieldUssClassName
The stop location field styling class.
Declaration
public const string locationFieldUssClassName = "appui-gradienteditor__location-field"
Field Value
| Type | Description |
|---|---|
| string |
markerChipUssClassName
The stop marker chip styling class. This is the part tinted with the stop's own value.
Declaration
public const string markerChipUssClassName = "appui-gradienteditor__marker-chip"
Field Value
| Type | Description |
|---|---|
| string |
markerUssClassName
The stop marker styling class.
Declaration
public const string markerUssClassName = "appui-gradienteditor__marker"
Field Value
| Type | Description |
|---|---|
| string |
maxKeys
The most keys a Gradient can hold on either rail. A ninth key is silently dropped by SetKeys(GradientColorKey[],GradientAlphaKey[]), so the editor refuses to add one instead.
Declaration
public const int maxKeys = 8
Field Value
| Type | Description |
|---|---|
| int |
minKeys
The fewest keys a Gradient can hold on either rail.
Declaration
public const int minKeys = 2
Field Value
| Type | Description |
|---|---|
| int |
Remarks
SetKeys(GradientColorKey[],GradientAlphaKey[]) silently pads anything shorter back up to two, so a rail that let the user delete down to one key would appear to work and then hand back a gradient with a duplicated key. The editor refuses the deletion instead.
modeDropdownUssClassName
The gradient mode dropdown styling class.
Declaration
public const string modeDropdownUssClassName = "appui-gradienteditor__mode-dropdown"
Field Value
| Type | Description |
|---|---|
| string |
rowUssClassName
The inspector row styling class.
Declaration
public const string rowUssClassName = "appui-gradienteditor__row"
Field Value
| Type | Description |
|---|---|
| string |
selectedMarkerUssClassName
The selected stop marker styling class.
Declaration
public const string selectedMarkerUssClassName = "appui-gradienteditor__marker--selected"
Field Value
| Type | Description |
|---|---|
| string |
stripUssClassName
The preview strip styling class.
Declaration
public const string stripUssClassName = "appui-gradienteditor__strip"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The GradientEditor main styling class.
Declaration
public const string ussClassName = "appui-gradienteditor"
Field Value
| Type | Description |
|---|---|
| string |
Properties
alphaElement
The ColorSlider editing the selected alpha stop.
Declaration
public ColorSlider alphaElement { get; }
Property Value
| Type | Description |
|---|---|
| ColorSlider |
alphaMarkers
The alpha stop markers, in time order.
Declaration
public IReadOnlyList<VisualElement> alphaMarkers { get; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyList<VisualElement> |
alphaRailElement
The rail carrying the alpha stops, above the strip.
Declaration
public VisualElement alphaRailElement { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
colorElement
The ColorField editing the selected color stop.
Declaration
public ColorField colorElement { get; }
Property Value
| Type | Description |
|---|---|
| ColorField |
colorMarkers
The color stop markers, in time order.
Declaration
public IReadOnlyList<VisualElement> colorMarkers { get; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyList<VisualElement> |
colorRailElement
The rail carrying the color stops, below the strip.
Declaration
public VisualElement colorRailElement { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
contentContainer
Logical container where child elements are added. If a child is added to this element, the child is added to this element's content container instead.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
hdr
Whether the color stop editor offers HDR colors. Forwarded from hdr when the editor is opened as its picker.
Declaration
[CreateProperty]
public bool hdr { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
locationElement
The stop location field, in percent.
Declaration
public IntField locationElement { get; }
Property Value
| Type | Description |
|---|---|
| IntField |
mode
How the gradient interpolates between its color stops.
Declaration
[CreateProperty]
public GradientMode mode { get; set; }
Property Value
| Type | Description |
|---|---|
| GradientMode |
Remarks
The dropdown offers Blend and Fixed, matching the registry. PerceptualBlend can still be set from code; it renders as a blend.
modeElement
The gradient mode dropdown.
Declaration
public Dropdown modeElement { get; }
Property Value
| Type | Description |
|---|---|
| Dropdown |
selectedKeyIndex
The index of the selected stop on its rail, or -1 when nothing is selected.
Declaration
public int selectedKeyIndex { get; }
Property Value
| Type | Description |
|---|---|
| int |
selectedKeyKind
Which rail the selected stop lives on.
Declaration
public GradientEditor.KeyKind selectedKeyKind { get; }
Property Value
| Type | Description |
|---|---|
| GradientEditor.KeyKind |
stripElement
The ColorSwatch rendering the composited preview.
Declaration
public ColorSwatch stripElement { get; }
Property Value
| Type | Description |
|---|---|
| ColorSwatch |
value
The gradient being edited.
Declaration
[CreateProperty]
public Gradient value { get; set; }
Property Value
| Type | Description |
|---|---|
| Gradient |
Remarks
The getter builds a fresh Gradient on every read, so a consumer holding an earlier value never sees it mutate underneath them.
Methods
AddAlphaKey(float)
Adds an alpha stop, sampling the current alpha ramp at that time, then selects it.
Declaration
public bool AddAlphaKey(float time)
Parameters
| Type | Name | Description |
|---|---|---|
| float | time | Normalized time, clamped to 0..1. |
Returns
| Type | Description |
|---|---|
| bool | False when the rail already holds maxKeys stops. |
AddColorKey(float)
Adds a color stop, sampling the current gradient at that time so the gradient looks unchanged, then selects it.
Declaration
public bool AddColorKey(float time)
Parameters
| Type | Name | Description |
|---|---|---|
| float | time | Normalized time, clamped to 0..1. |
Returns
| Type | Description |
|---|---|
| bool | False when the rail already holds maxKeys stops. |
Dismiss()
Closes the popover this editor was opened in, if any.
Declaration
public void Dismiss()
RemoveSelectedKey()
Removes the selected stop.
Declaration
public bool RemoveSelectedKey()
Returns
| Type | Description |
|---|---|
| bool | False when nothing is selected, or when its rail is already down to minKeys stops. |
SelectKey(KeyKind, int)
Selects a stop.
Declaration
public void SelectKey(GradientEditor.KeyKind kind, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| GradientEditor.KeyKind | kind | Which rail the stop lives on. |
| int | index | Index of the stop on that rail. Out-of-range clears the selection. |
SetSelectedKeyAlpha(float)
Sets the alpha of the selected alpha stop. Does nothing when a color stop is selected.
Declaration
public void SetSelectedKeyAlpha(float alpha)
Parameters
| Type | Name | Description |
|---|---|---|
| float | alpha | The new alpha, clamped to 0..1. |
SetSelectedKeyColor(Color)
Sets the color of the selected color stop. Does nothing when an alpha stop is selected.
Declaration
public void SetSelectedKeyColor(Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The new stop color. Alpha is carried by the alpha rail, not here. |
SetSelectedKeyTime(float)
Moves the selected stop in time. The selection follows the stop through the reorder that may cause.
Declaration
public void SetSelectedKeyTime(float time)
Parameters
| Type | Name | Description |
|---|---|---|
| float | time | Normalized time, clamped to 0..1. |
SetValueWithoutNotify(Gradient)
Sets the gradient without sending a change event.
Declaration
public void SetValueWithoutNotify(Gradient newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| Gradient | newValue | The gradient to edit. Null resets to the default. |
Show(ColorGradientField, Action<Gradient>, Gradient, bool)
Opens the editor in a Popover anchored to the field, as ColorField does with ColorPicker.
Declaration
public void Show(ColorGradientField owner, Action<Gradient> onValueChanging, Gradient initialValue, bool showHdr)
Parameters
| Type | Name | Description |
|---|---|---|
| ColorGradientField | owner | The field that requested the editor. |
| Action<Gradient> | onValueChanging | Invoked as the user edits. |
| Gradient | initialValue | The gradient assigned when the editor opened. |
| bool | showHdr | Whether the editor should allow HDR color stops. |