UI Toolbox logoUI Toolbox
Support

Video director

A small video editor, as a game might have for its cutscenes or a tool for its trailers. A VideoView plays the sample clip, and a Timeline under it holds the script of the clip on six tracks: Titles, Captions, Text FX, Picture, Volume and Sounds. A palette adds items, a PropertyGrid in the inspector edits the selected item, and each edit goes into an undo history. The video is the one clock: the playhead follows it, a move of the playhead seeks it, and the preview shows each change at once.

Video director: the caption "Life is good." over the bunny clip, with the six tracks of the timeline under it

Scene: Assets/UIToolbox/Examples/Scenes/DirectorDemo.unity. Folder: Assets/UIToolbox/Examples/DirectorDemo.

Try it

  1. Open the scene. The video waits at the start.
  2. Click the round play button, or press Space. The video plays. The picture fades in from black, the title "The bunny wakes up" waves in its gradient colors, and a chime sounds. The playhead moves with the video, and the time and the volume under the picture count up.
  3. Watch the picture. The captions type in one after the other, each with its own entry and tags. The picture shakes when the bunny wakes up, zooms in on the yawn, takes a warm tint for "Life is good." and flashes white at the flowers. The sound markers play a boing, a whoosh, a pop and a drum.
  4. Click or drag on the ruler of the timeline. The video seeks to that time, and the titles, captions, effects and volume of that time show at once.
  5. Click the Caption tile in the palette. A caption item starts at the playhead and is selected. The inspector shows its fields.
  6. Change its Text in the inspector, for example to Hello <wave>there</wave>!. The caption over the picture and the name on the timeline change as you type. Change its Entry and its Speed.
  7. Drag the Picture tile onto the Picture track. A picture effect starts where you drop it. In the inspector, set its Effect to Tint and pick a Tint color. Drag Amount to make it stronger.
  8. Double-click an empty part of the Sounds lane. A sound marker goes there. Pick its Sound in the inspector: it plays once.
  9. Drag an item to move it, or its edge to change its length. Press Delete to remove the selected item.
  10. Click Undo, or press Ctrl+Z. The last edit goes back. Redo, or Ctrl+Y, does it again. Many quick changes of one field, as a slider drag makes, are one step.
  11. Select an item and click Duplicate. A copy starts where the item ends.
  12. Drag the Zoom slider. The timeline shows a part of the clip around the playhead, and the playback pages the view to keep the playhead in it. Fit shows all of it again.
  13. Drag on an empty lane to pick a range, then turn on Loop and play. The range plays again and again. Without a range, Loop plays the clip again at its end.
  14. Click a line of the script on the right. The timeline selects its caption, and the video seeks to it.
  15. Open How it is made under the timeline for the key code, or click Show me for a tour.

What it shows

The six tracks

Track Items What an item does Its fields
Titles 2 Shows its text in the middle of the picture, with its effects. Text, Effects
Captions 7 Types its text at the bottom of the picture. The text holds effect tags, such as <shake> and <highlight=#ffd54a>. Text, Entry, Speed
Text FX 5 Adds its effects to the whole caption while it runs. Two items at the same time add both. Effects
Picture 6 Zooms or shakes the video, fades it in from black or out to black, flashes it white, or tints it. Effect, Amount, Tint
Volume 6 Fades the volume from the level of the key before it to its own level, over its length. After the key, the volume stays at its level. Before the first key, the volume is 0. Level
Sounds 5 A marker: a short sound plays when the playhead passes it. Sound, Loudness

Each item also shows its Start in the inspector, and each item with a length shows its Length. The Tint field is on only for the Tint effect.

The cues

Each item keeps what it does in a Cue in its userData: a TitleCue, a CaptionCue, a TextFxCue, a PictureCue, a VolumeCue or a SoundCue. The inspector is a PropertyGrid with the cue of the selected item as its Target, so the rows come from the public fields of the cue class. [Category], [Description], [Range] and [EnabledIf] shape the rows. Start and Length are properties that move and size the TimelineItem and keep it inside the clip.

The palette

A tile is a VisualElement with a DragSource. A click on a tile adds an item at the playhead. A drag shows a copy of the tile under the pointer, and the DropTarget of the timeline adds the item at the time where it drops. A double-click on an empty lane adds an item of the kind of the lane, using TrackAt and TimeAt of the timeline.

Undo and redo

After each edit the controller keeps one state of all the items: the place and the length of each item, and a copy of its cue. Undo puts the items back as the state before. The edits are the palette, the inspector, Duplicate, and the drags and removals that the timeline reports with ItemEdited. Ctrl+Z and Ctrl+Y work everywhere in the demo, also in the fields of the inspector. The history keeps the last 100 edits.

One clock

The video is the clock. Its TimeChanged event sets the playhead of the timeline. A drag on the ruler moves the playhead, and its PlayheadMoved event seeks the video. PlayheadMoved also fires when code sets Playhead, so the controller sets a flag while the video moves the playhead and skips the seek then.

The Space key of a focused timeline starts the playback of the timeline itself. The controller pauses the timeline in its PlayingChanged event and plays the video instead, so the two never run on two clocks.

At each tick the controller plays the sound markers between the last time and the new time. A seek jumps, so it plays none.

No garbage in playback

Each part of the preview changes only when its value changes. The searches for the items under a time walk each track with an index loop. The texts of the time and the volume come from tables made once, so a frame of playback builds no string.

How it is built

File Does
DirectorDemo.uxml The window: the stage with the video and its layers, the play bar, the inspector card, the script list, the tools row, the timeline, the keys, the code and the tour.
DirectorDemo.uss The layout, the layers over the picture, the palette tiles, and the track colors as custom properties that read the theme tokens.
DirectorDemoController.cs The cues and the script of the clip, the tracks, the clock, the preview, the sounds, the palette, the inspector, the tools and the undo history.

A cue is a plain class. The inspector shows its public fields:

public sealed class CaptionCue : SpanCue
{
    [Category("Caption"), Description("How the letters come in.")]
    public TextEntry Entry = TextEntry.Fade;

    [Category("Caption"), Range(5, 120)]
    public float Speed = 30;
}

The selected item sets the target of the grid, and each change of the grid names the item again, shows the preview again and keeps a state for undo:

_timeline.ItemSelected += item => _grid.Target = CueOf(item);
_grid.PropertyValueChanged += (row, old, now) =>
{
    edited(cue.Item);
    commit(cue, row.Path);
};

The video moves the playhead, and the user moves the video:

public void ShowAt(float time)
{
    _fromVideo = true;
    _timeline.Playhead = time;
    _fromVideo = false;
    show(time, false);
}

private void onPlayheadMoved(float time)
{
    if (_fromVideo) return;
    _video.Seek(time);
    show(time, false);
}

A palette tile carries its kind to the timeline:

tile.AddManipulator(new DragSource(() => kind));
_timeline.AddManipulator(new DropTarget(
    data => AddCue((CueKind)data, _timeline.TimeAt(_dropAt)),
    data => data is CueKind));

The track colors come from the theme. DirectorDemo.uss sets six custom properties on the timeline from the theme tokens, and the controller reads them in a CustomStyleResolvedEvent, so a change of theme colors the tracks again:

.dir .dir-timeline {
    --dir-titles: var(--tb-color-warning);
    --dir-captions: var(--tb-color-accent);
    --dir-textfx: var(--tb-color-danger);
    --dir-picture: var(--tb-color-text-muted);
    --dir-volume: var(--tb-color-success);
    --dir-sounds: var(--tb-color-text);
}

Use it in your game

Credits

The clip is 30 seconds of the short film Big Buck Bunny, (c) copyright 2008, Blender Foundation / www.bigbuckbunny.org, under the Creative Commons Attribution 3.0 license (CC BY 3.0). The window shows the credit under the play bar. Keep it when you ship the clip, or play a clip of your own.

See also