Class Vector2Field
A numeric input field for editing 2D vector values with X and Y components.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class Vector2Field : BaseVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder, IInputElement<Vector2>, IValidatableElement<Vector2>, ISizeableElement, INotifyValueChanging<Vector2>, INotifyValueChanged<Vector2>, IFormattable<float>
Remarks
Vector2Field is a specialized input component that allows users to edit 2D vector values by providing separate numeric fields for X and Y coordinates. It's particularly useful for position, scale, or any other 2D vector properties in Unity applications.
The component consists of two numeric input fields arranged horizontally - one for the X coordinate and one for the Y coordinate. Each field can be independently edited and supports numerical input with floating-point precision.
TIP: Use Vector2Field when you need to edit 2D coordinates, sizes, or any other values that naturally come in pairs of X/Y components.
Examples
Basic usage in UXML. Creating a medium-sized Vector2Field with default value in UXML.
<Vector2Field name="position-field" size="M" value="0,0" />
Code usage with validation. Creating a Vector2Field with formatting, validation, and change handling.
var vector2Field = new Vector2Field();
vector2Field.formatString = "F1";
vector2Field.validateValue = (v) => v.magnitude <= 100f;
vector2Field.RegisterValueChangedCallback(evt => {
Debug.Log($"New value: {evt.newValue}");
});
Using with data binding. Binding the Vector2Field to a Transform's position.
vector2Field.value = transform.position;
vector2Field.RegisterValueChangedCallback(evt => {
transform.position = evt.newValue;
});
Constructors
Vector2Field()
Default constructor.
Declaration
public Vector2Field()
Fields
containerUssClassName
The Vector2Field container styling class.
Declaration
public const string containerUssClassName = "appui-vector2field__container"
Field Value
| Type | Description |
|---|---|
| string |
sizeUssClassName
The Vector2Field size styling class.
Declaration
public const string sizeUssClassName = "appui-vector2field--size-"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The Vector2Field main styling class.
Declaration
public const string ussClassName = "appui-vector2field"
Field Value
| Type | Description |
|---|---|
| string |
xFieldUssClassName
The Vector2Field X NumericalField styling class.
Declaration
public const string xFieldUssClassName = "appui-vector2field__x-field"
Field Value
| Type | Description |
|---|---|
| string |
yFieldUssClassName
The Vector2Field Y NumericalField styling class.
Declaration
public const string yFieldUssClassName = "appui-vector2field__y-field"
Field Value
| Type | Description |
|---|---|
| string |
Properties
contentContainer
The content container of the Vector2Field.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
formatFunction
The format function of the element.
Declaration
[CreateProperty]
public FormatFunction<float> formatFunction { get; set; }
Property Value
| Type | Description |
|---|---|
| FormatFunction<float> |
formatString
The format string of the element.
Declaration
[CreateProperty]
[UxmlAttribute]
public string formatString { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
invalid
The invalid state of the Vector2Field.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool invalid { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
size
The size of the Vector2Field.
Declaration
[CreateProperty]
[UxmlAttribute]
public Size size { get; set; }
Property Value
| Type | Description |
|---|---|
| Size |
validateValue
The validation function to use to validate the value.
Declaration
[CreateProperty]
public Func<Vector2, bool> validateValue { get; set; }
Property Value
| Type | Description |
|---|---|
| Func<Vector2, bool> |
value
The value of the Vector2Field.
Declaration
[CreateProperty]
[UxmlAttribute]
public Vector2 value { get; set; }
Property Value
| Type | Description |
|---|---|
| Vector2 |
Methods
GetSizeUssClassName(Size)
Declaration
public static string GetSizeUssClassName(Size enumValue)
Parameters
| Type | Name | Description |
|---|---|---|
| Size | enumValue |
Returns
| Type | Description |
|---|---|
| string |
SetValueWithoutNotify(Vector2)
Set the value of the Vector2Field without notifying the change.
Declaration
public void SetValueWithoutNotify(Vector2 newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | newValue | The new value of the Vector2Field. |