Class TimeField
A form input control that allows users to enter a time of day through text input or a spinner picker.
Inheritance
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class TimeField : ExVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder, IInputElement<TimeSpan>, IValidatableElement<TimeSpan>, INotifyValueChanging<TimeSpan>, INotifyValueChanged<TimeSpan>, ISizeableElement
Remarks
The TimeField component combines a text input with a TimePicker popover. Users can type the time directly, step the segment under the caret with the Up and Down arrow keys, hop between segments with Left and Right, or click the clock icon to spin the value in a picker.
The value is a TimeSpan holding a wall-clock time of day, always normalized into
[00:00:00, 24:00:00). It is deliberately not a DateTime: a TimeField carries no
calendar date. Use DateTimeField when you need both.
Key features:
- Segmented text input with per-segment stepping and within-unit wrapping
- Spinner picker interface
- 12-hour and 24-hour clocks
- Optional seconds segment and a configurable minute step
- Size variants
- Value validation
NOTE: The display format is derived from hour12 and showSeconds and is always parsed and formatted with the invariant culture, the same way DateField handles its own format string.
Examples
Basic usage in UXML:
<UXML xmlns:appui="Unity.AppUI.UI">
<appui:TimeField name="alarm" />
</UXML>
A 12-hour field with seconds and a five minute step:
<UXML xmlns:appui="Unity.AppUI.UI">
<appui:TimeField
name="meeting-time"
size="Lg"
hour12="true"
show-seconds="true"
minute-step="5"
value="09:30:00" />
</UXML>
Creating and validating a TimeField in C#:
var timeField = new TimeField
{
value = new TimeSpan(9, 0, 0),
minuteStep = 15,
size = Size.Md
};
// Office hours only.
timeField.validateValue = time => time >= new TimeSpan(9, 0, 0) && time <= new TimeSpan(18, 0, 0);
timeField.RegisterValueChangedCallback(evt => {
Debug.Log($"Selected time: ");
});
Constructors
TimeField()
Default constructor.
Declaration
public TimeField()
Fields
inputUssClassName
The TimeField input styling class.
Declaration
public static readonly string inputUssClassName
Field Value
| Type | Description |
|---|---|
| string |
pickerButtonUssClassName
The TimeField picker button styling class.
Declaration
public static readonly string pickerButtonUssClassName
Field Value
| Type | Description |
|---|---|
| string |
sizeUssClassName
The TimeField size styling class.
Declaration
public static readonly string sizeUssClassName
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The TimeField main styling class.
Declaration
public static readonly string ussClassName
Field Value
| Type | Description |
|---|---|
| string |
Properties
contentContainer
The content container of this TimeField. This is null for TimeField.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
formatString
The format string used to display and parse the value, derived from hour12 and showSeconds.
Declaration
public string formatString { get; }
Property Value
| Type | Description |
|---|---|
| string |
hour12
Whether the field uses a 12-hour clock with an AM/PM segment. Defaults to false, a 24-hour clock.
Declaration
[CreateProperty]
[UxmlAttribute("hour12")]
public bool hour12 { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
invalid
The TimeField invalid state.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool invalid { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
minuteStep
The number of minutes stepped per arrow press or spinner click. Defaults to 1.
Declaration
[CreateProperty]
[UxmlAttribute("minute-step")]
public int minuteStep { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
Values below 1 are clamped to 1.
showSeconds
Whether the field displays a seconds segment. Defaults to false.
Declaration
[CreateProperty]
[UxmlAttribute("show-seconds")]
public bool showSeconds { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
size
The TimeField size.
Declaration
[CreateProperty]
[UxmlAttribute]
public Size size { get; set; }
Property Value
| Type | Description |
|---|---|
| Size |
validateValue
The TimeField validation function.
Declaration
[CreateProperty]
public Func<TimeSpan, bool> validateValue { get; set; }
Property Value
| Type | Description |
|---|---|
| Func<TimeSpan, bool> |
value
The TimeField value, a time of day in [00:00:00, 24:00:00).
Declaration
[CreateProperty]
[UxmlAttribute]
public TimeSpan value { get; set; }
Property Value
| Type | Description |
|---|---|
| TimeSpan |
Remarks
Values outside that range are wrapped into it, so 25:00 becomes 01:00 and a negative
span counts back from midnight.
Methods
SetValueWithoutNotify(TimeSpan)
Sets the TimeField value without notifying the TimeField.
Declaration
public void SetValueWithoutNotify(TimeSpan newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | newValue | The new TimeField value. It is normalized into |
StepSegment(TimeSegment, int)
Steps a single segment of the value, wrapping inside that segment's own unit.
Declaration
public void StepSegment(TimeSegment segment, int delta)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSegment | segment | The segment to step. |
| int | delta | The number of steps, negative to step down. |
TryParse(string, out TimeSpan)
Tries to parse a string into a time of day, using the field's current format first and then a set of common fallbacks.
Declaration
public bool TryParse(string str, out TimeSpan parsed)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The string to parse. |
| TimeSpan | parsed | The parsed time of day, normalized into |
Returns
| Type | Description |
|---|---|
| bool | True if the string could be parsed, false otherwise. |