Timeline
Timeline in UIToolbox shows TimelineTrack rows of TimelineItem spans and markers on a time ruler, with a playhead.
Video tutorial: Timeline. All videos: Video tutorials.
<tb:Timeline name="edit" duration="60" time-format="0.#'s'" style="height: 150px;">
<tb:TimelineTrack label="Video">
<tb:TimelineItem label="Intro" start="0" duration="8" />
<tb:TimelineItem label="Scene A" start="8" duration="17" color="#3CB371" />
</tb:TimelineTrack>
<tb:TimelineTrack label="Events">
<tb:TimelineItem label="Cut" start="8" /> <!-- no duration: a marker -->
</tb:TimelineTrack>
</tb:Timeline>
var timeline = root.Q<Timeline>("edit");
timeline.GetTrack("Events").AddMarker("Fade", 25);
timeline.ItemReached += item => Debug.Log($"{item.Label} at {item.Start}");
timeline.SelectionChanged += range => Debug.Log(range);
timeline.Play();
| Member | Description |
|---|---|
TimelineItem.Label (label), Start (start), Duration (duration), Color (color) |
The name, the start time, the length (0 makes a marker), and the fill (clear takes the track color). A change moves the item. |
TimelineItem.End, IsMarker, DisplayColor, Track, Timeline |
The end time, the marker test, the drawn color, and the parents. |
TimelineTrack.Label (label), Color (color), Items, AddItem, AddMarker |
The header text, the item color (clear takes the PieChart.Palette color by position), and the items. |
Tracks, Items, AddTrack, GetTrack, Refresh |
The tracks and all their items. Call Refresh() after you add or remove tracks or items with Add or Remove. |
ScrollToTrack |
Scrolls the tracks so that the track shows. Tracks that do not fit the height scroll up and down under the ruler. |
Duration (duration, 60), TimeFormat (time-format, "0.##"), FormatTime |
The length of the ruler, and the number format of the ruler and the tooltips. |
TimeFormatter, TickFormatter |
Functions that make the text of a time. TimeFormatter is for the tooltips, FormatTime and the ruler; TickFormatter is for the ruler only. Null (the default) uses TimeFormat. See Days and hours. |
TickSteps (tick-steps), MajorStep (major-step, 0) |
The steps that the ruler can use between two ticks, and the step of the major ticks. Without TickSteps the ruler uses round decimal steps (1, 2, 5, 10). |
Playhead (playhead), PlayheadMoved |
The playhead time, from 0 to Duration. |
Play, Pause, IsPlaying, PlayingChanged, PlaybackSpeed (playback-speed, 1), Loop (loop) |
Playback moves the playhead in real time. At the end it stops, or goes back to 0 with Loop. Play at the end starts over. A zoomed view turns the page when the playhead runs off it. |
ItemReached |
Sent during playback when the playhead reaches the start of an item. |
Selection, SetSelection, ClearSelection, SelectionChanged |
The selected range as (start, end), or null. |
SelectedItem, SelectItem, ItemSelected |
The selected item, or null. |
ViewStart, ViewEnd, IsZoomed, SetView, ResetView, ViewChanged |
The shown range. It stays inside 0 to Duration. |
TickTimes, TimeAt(worldPosition) |
The ruler tick times now shown, and the time at a panel position. |
Interactive, ZoomPan (zoom-pan) |
Clicks, drags and keys; the wheel and the pan. On by default. |
EditItems (edit-items), ItemChanged |
Drags move and resize the items. Off by default. ItemChanged is sent once after a drag that changed the start, the length or the track; a change by code does not send it. |
Snap (snap, 0), SnapToItems (snap-to-items, true) |
A dragged edge goes to a multiple of Snap (0 turns the step off). With SnapToItems, an edge within 8 pixels of the playhead or of the edge of another item goes to it first. |
- Drag in the ruler to move the playhead. Drag in a lane to select a range. A click on an empty lane clears both selections.
- A click on an item selects it. The tooltip shows "Label: start to end", or "Label: time" for a marker.
- In the UI Builder, a timeline without tracks shows two sample tracks in a dashed outline. The sample does not show in Play mode or in a build, and the UI Builder does not save it to the UXML file.
- With
EditItems, a drag on the body of an item moves it, also to the track under the pointer. A drag on the 6-pixel edge of a span moves that edge; a span keeps at least one snap step. A marker has no edges. Escape during a drag puts the item back. Items can overlap. - With
EditItems, the pointer over the edge of a span shows a left-right arrow cursor. A runtime panel shows only texture cursors, so the timeline draws a 32-pixel arrow (Timeline.ResizeCursor). The cursor stays during the drag. - The timeline takes focus. Left and Right (the arrow keys or the gamepad) move the playhead by one ruler tick. Home and End go to the ends. Space plays and pauses. Escape clears the selections.
- With focus, the wheel over the ruler or a lane zooms around the pointer. Shift+wheel and a horizontal wheel pan. A middle-button drag pans. A double-click in the ruler resets the view.
- Without focus, and over the track names, the wheel scrolls the tracks. The ruler lane ends before the vertical scroll bar, so it keeps the width of the track lanes.
- Set
--tb-timeline-playhead,--tb-timeline-selection,--tb-timeline-gridand--tb-timeline-grid-majoron.tb-timelineto change the playhead, the range fill, the grid lines and the grid lines of the major ticks.
Days and hours
The time of a timeline is a number. To show a schedule, let one unit be one hour and give the ruler the steps of a clock.
<tb:Timeline name="week" duration="168" tick-steps="0.5,1,2,3,6,12,24" major-step="24" snap="0.5" edit-items="true" />
string[] days = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
string day(float hours) => days[(int)(hours / 24) % 7];
string clock(float hours) => $"{(int)hours % 24:00}:{(int)(hours * 60) % 60:00}";
week.TimeFormatter = hours => $"{day(hours)} {clock(hours)}"; // tooltips: "Tue 14:30"
week.TickFormatter = hours => hours % 24 == 0 ? day(hours) : clock(hours); // ruler: "Tue", "06:00", "12:00"
week.SetView(24, 48); // show Tuesday
tick-stepskeeps the ticks on clock times. The ruler takes the smallest step that gives the ticks room: a day for the whole week, 3 hours for one day, less after a zoom. A range too long for the largest step takes a multiple of it.major-step="24"makes each midnight a major tick: a bold label, a tick of the full ruler height and a stronger grid line.snap="0.5"puts a dragged shift on a half hour.- The Timeline demo of the examples (
TimelineDemo.unity) shows a crew schedule made this way.
Styling
| Class | Element |
|---|---|
tb-timeline |
The timeline (column, grows; accent border when it has focus). --tb-timeline-playhead, --tb-timeline-selection, --tb-timeline-grid and --tb-timeline-grid-major set the overlay colors. tb-timeline--playing is added during playback. |
tb-timeline__ruler |
The ruler row (surface-alt background). |
tb-timeline__header |
The ruler corner and each track header. They share a width (90 px), so the lanes line up with the ruler. |
tb-timeline__ruler-lane |
The tick area of the ruler. Its text color sets the tick lines. |
tb-timeline__tick-label, --major |
A ruler time (small muted text); a major tick (bold, text color). |
tb-timeline__tracks |
The column of tracks: a vertical ScrollView. The scroll bar rules of the toolbox apply to it. |
tb-timeline__track |
A track row (30 px). |
tb-timeline__lane |
The item area of a track. It clips the items. |
tb-timeline__item, --marker, --selected, --dragging |
A span (the script sets its position and color); a marker (a diamond on its time, name in the tooltip); the selected item (text-color border); the item during a drag (0.8 opacity). |
tb-timeline__item-label |
The name on a span (white, ellipsis). |
tb-timeline__overlay |
The grid lines, the range fill and the playhead, over the ruler and the lanes. |