UI Toolbox logoUI Toolbox
Support

VideoView

A VideoView plays a video in UI Toolkit. It wraps Unity's VideoPlayer: it plays a VideoClip or a URL into a RenderTexture of the size of the video, and shows the picture in the view. A bar under the picture has play and pause, a seek slider with the time and the length, a volume button, loop, the playback speed and full screen. The volume button opens a popup with a volume slider and a mute toggle under it. The speed button shows the current speed ("1x") and opens a menu of the speeds. The bar is made of toolbox controls (ToggleButton, MediaIcon, Slider, Toggle, ContextMenuComponent), so it takes the theme. Use it for a cutscene, a tutorial clip, a trailer in the main menu or a video log in a window.

A VideoView at 0:14 of 0:30, with the pause, seek, volume, loop, speed and full screen buttons under the pictureA VideoView at 0:14 of 0:30, with the pause, seek, volume, loop, speed and full screen buttons under the picture

<tb:VideoView
    clip="project://database/Assets/Videos/Intro.mp4?..."
    volume="0.8" />
<tb:VideoView url="Videos/Intro.mp4" autoplay="true" loop="true" />

In UI Builder, pick the clip in the Clip field of the inspector; it writes the project://database address.

var video = new VideoView { Clip = introClip, Autoplay = true };
video.Finished += startGame;
panel.Add(video);

video.Seek(30);
video.Volume = 0.5f;
video.PlaybackSpeed = 1.5f;
Member Description
Clip (clip) The video asset to play. It wins over Url.
Url (url) The video file or address when there is no clip: a URL (https://, file://), a full path, or a path in StreamingAssets such as Videos/Intro.mp4. See ResolveUrl.
Autoplay (autoplay) Plays as soon as the video is ready. Off by default.
Loop (loop) Starts again at the end, with no Finished event. Off by default.
Muted (muted) Silences the sound and keeps the volume.
Volume (volume) The sound volume, from 0 to 1. 1 by default.
PlaybackSpeed (playback-speed) The speed of play, from 0.1 to 10; 1 is the normal speed. The speed menu of the bar offers 0.5x, 1x, 1.5x and 2x (VideoView.Speeds), with a dot at the current speed. Some sources cannot change speed (VideoPlayer.canSetPlaybackSpeed); the view keeps the value and sets it again when the video is ready.
ShowControls (show-controls) Shows the bar. On by default. Off, the picture takes all the view: make your own bar with the members below.
WaveformClip (waveform-clip) An audio clip of the sound of the video. A WaveformBar with its peaks takes the place of the seek slider. See A waveform seek bar.
Waveform The WaveformBar of the bar while WaveformClip is set, to add markers or change its style.
ScaleMode (scale-mode) A VideoScaleMode: Fit shows the whole picture with bars on two sides; Fill covers the view and cuts the parts outside it; Stretch takes the size of the view and can change the shape of the picture. Fit by default.
Player The VideoPlayer. See The player.
Texture The RenderTexture the video plays into, or null before the size of the video is known.
IsPlaying True from Play to Pause, the end or an error. It is true while the video gets ready to play.
IsPreparing True while the player opens the video. The view shows a spinning ProgressRing then.
Time The position in seconds. While a seek runs, the time it goes to.
Duration The length in seconds: of the open video, else of the clip, else 0.
Maximized True while the view shows full screen.
Play() Plays from the current time; at the end, from the start. Before the video is ready, it plays when it is.
Pause() Pauses, and raises Paused.
Stop() Pauses and goes back to the start.
TogglePlay() Plays or pauses.
Seek(seconds) Goes to a time, clamped to the video. Before the video is ready, it goes there when it is. While the player decodes a seek, a new seek waits for it, and only the last time waits: a drag on the seek slider does not queue a frame for each pointer move. A paused view shows the frame of the new time, also before the first play.
Prepared The video is open: its size and length are known.
Started The video starts to play.
Paused After Pause and Stop.
Finished The video played to its end, with Loop off.
TimeChanged The position changed, by play or by a seek. The argument is the time in seconds.
SoundChanged Volume or Muted changed, by the code, the bar, the arrow keys, M or a gamepad.
ResolveUrl(url) The address the player gets for a Url: a URL or a full path stays, any other path goes under Application.streamingAssetsPath.
FormatTime(seconds) The time as m:ss, or h:mm:ss from an hour, as in the bar.

Keys and pointer

Input Action
A click on the picture Plays or pauses, and gives the view the focus.
A double click on the picture Full screen, or back.
Space Plays or pauses.
A on a gamepad Plays or pauses.
X on a gamepad Starts the seek and volume mode, and leaves it.
Left, Right Seek 5 seconds back or forward (VideoView.SEEK_STEP). On a gamepad, the D-pad does the same in the seek and volume mode.
Up, Down The volume up or down by 0.1 (VideoView.VOLUME_STEP). On a gamepad, the D-pad does the same in the seek and volume mode.
M Mute or sound. The mute toggle in the popup of the volume button does the same.
A drag on the seek slider Pauses the player during the drag, so the picture follows the thumb. The play goes on at the release.
Escape in the volume popup Closes the popup.
F Full screen, or back.
Escape Leaves full screen.

Space, the arrows and F work while the view itself has the focus. Tab moves into the bar, where each control takes its usual keys.

On a gamepad, the D-pad and the left stick move the focus, and A plays or pauses. X starts the seek and volume mode: the view gets the class tb-editing (a wider focus ring), Left and Right seek, and Up and Down change the volume. X, A or B leaves the mode and keeps the time and the volume, so B does not undo a seek.

The player

With no Player, the view makes one on a hidden GameObject (HideFlags.HideAndDontSave) when it attaches to a panel in play mode with a clip or a URL. When the view leaves the panel, it destroys that object and releases and destroys the texture. So the view needs no setup in the scene, stays a plain element of a UXML file, and leaves nothing behind when its window closes. In the editor out of play mode, such as in UI Builder, the view makes no player and shows a black area.

The view sets up its own player for direct audio output with one track, so Volume and Muted go to SetDirectAudioVolume and SetDirectAudioMute.

Set Player to use a VideoPlayer of your own, for example one with an AudioSource output for 3D sound or a mixer. The view then sets its render mode, its target texture, playOnAwake, waitForFirstFrame, skipOnDrop and isLooping, and keeps its audio output. When the view leaves the panel, it stops your player and clears its target texture, but does not destroy it. With an AudioSource output, set the volume on the AudioSource.

Limits:

Full screen

Full screen moves the view into the nearest Workspace, or to the root of the panel when there is none, over all the rest, with the class tb-video-view--maximized. The view keeps the look classes of its old place (tb-style-scope). Back, the view goes to its old place and size. The player keeps playing through the move.

A waveform seek bar

Set WaveformClip to the sound of the video saved as an audio clip, and the bar shows its peaks as a WaveformBar in the place of the slider. A click or a drag on the waveform seeks, a drag pauses the video as the slider does, and the played part follows the video. The view sets the Duration of the waveform to the length of the video, so markers and the hover time are in the time of the video.

<tb:VideoView
    clip="..."
    waveform-clip="project://database/Assets/Audio/IntroSound.wav?..." />
video.WaveformClip = introSound;
video.Waveform.AddMarker(42, "Boss");
video.Waveform.WaveStyle = WaveformStyle.Line;

Set the clip to null to show the slider again.

Limitation: the view cannot draw the waveform of the sound inside a video file. Unity's video player gives the sound only while it plays, so the waveform needs a clip of its own. Save the sound of the video as an audio file, as the VideoDemo does with VideoSample.ogg. To read the sound of a video at runtime, use an audio decoder of your own (Unity has none for video files), make a clip from its samples and give it to the view:

var clip = AudioClip.Create("Video sound", samples.Length / channels,
    channels, frequency, false);
clip.SetData(samples, 0);
video.WaveformClip = clip;

A bar of your own

Set show-controls="false" and drive the view from your own controls. MediaIcon draws the same icons as the bar:

<tb:VideoView name="video" show-controls="false" clip="..." />
<ui:Button name="play">
    <tb:MediaIcon name="playIcon" kind="Play" />
</ui:Button>
<tb:ProgressRing name="ring" animate="false" />
play.clicked += video.TogglePlay;
video.Started += () => playIcon.Kind = MediaIconKind.Pause;
video.Paused += () => playIcon.Kind = MediaIconKind.Play;
video.TimeChanged += time => ring.Value = (float)(time / video.Duration);

A sound button can open a volume control of your own, made of toolbox parts. The custom bar of the VideoDemo has a pill that grows up from the sound button: a waveform MediaIcon at the top, a vertical Slider with its fill as the level track, and a mute button at the bottom, in the place of the sound button. AnchoredPopup.OpenBeside with a gap of minus the button height puts the bottom of the pill on the bottom of the button. A USS transition on height, which WindowAnimations.EnterAsync plays, grows the pill; with the animations of the workspace off, the pill opens at its full height at once. CloseOnEscape gives the focus back to the button. SoundChanged keeps the icons, the level and the dim fill right when the arrow keys, M or a gamepad change the sound:

var volume = new Slider(0, 1, SliderDirection.Vertical) { fill = true };
volume.AddToClassList("my-sound-level");
volume.RegisterValueChangedCallback(e => video.Volume = e.newValue);
var muteIcon = new MediaIcon(MediaIconKind.Volume);
var mute = new Button(() => video.Muted = !video.Muted);
mute.Add(muteIcon);
ToolboxText.SetTooltip(mute, ToolboxText.MUTE);
var pill = new VisualElement();
pill.AddToClassList("my-sound-pill");
pill.Add(new MediaIcon(MediaIconKind.Waveform));
pill.Add(volume);
pill.Add(mute);
var popup = new AnchoredPopup(pill) { CloseOnEscape = true };
popup.Closed += () => pill.RemoveFromClassList("my-sound-pill--open");
sound.clicked += () =>
{
    if (popup.IsOpen) { popup.Close(); return; }
    if (!popup.OpenBeside(sound, TooltipPlacement.Above, sound,
        -sound.layout.height)) return;
    // The slider moves its fill only on a panel, so the level goes into
    // it on each open
    volume.SetValueWithoutNotify(video.Volume);
    AsyncRunner.Run(() =>
        WindowAnimations.EnterAsync(pill, "my-sound-pill--grow",
        () => pill.AddToClassList("my-sound-pill--open")),
            "volume pill");
    mute.Focus();
};
video.SoundChanged += () =>
{
    var kind = video.Muted || video.Volume <= 0
        ? MediaIconKind.Muted : MediaIconKind.Volume;
    soundIcon.Kind = kind;
    muteIcon.Kind = kind;
    volume.SetValueWithoutNotify(video.Volume);
    pill.EnableInClassList("my-sound-pill--muted", video.Muted);
};
/* The pill opens at the size of the button; its parts sit at the bottom,
   so they show from the bottom up */
.my-sound-pill {
    width: 48px;
    height: 48px;
    border-radius: 24px;
    border-width: 2px;
    border-color: var(--tb-color-border);
    background-color: var(--tb-color-surface);
    justify-content: flex-end;
    align-items: center;
    overflow: hidden;
}
.my-sound-pill--open { height: 250px; }
.my-sound-pill--grow {
    transition-property: height;
    transition-duration: 0.2s;
}
.my-sound-pill--muted .unity-base-slider__fill { opacity: 0.35; }

The demo also puts a music note MediaIcon in the fill and hides it when the fill is too low for it, hides the dragger so that a press anywhere on the track sets the level, changes the level with Up, Down and the wheel anywhere in the pill, and closes the pill when the focus leaves it. See VideoDemoController and VideoDemo.uss.

MediaIcon

Twelve MediaIcon buttons in two rows: Play, Pause, Volume, Muted, Loop, Maximize, Restore, Backspace, Return, Eyedropper, Waveform and NoteTwelve MediaIcon buttons in two rows: Play, Pause, Volume, Muted, Loop, Maximize, Restore, Backspace, Return, Eyedropper, Waveform and Note

Member Description
Kind (kind) Play, Pause, Volume, Muted, Loop, Maximize, Restore, Backspace, Return, Eyedropper, Waveform, Note or Search.

The icon draws in its color, 16 px by default. In a button it draws again when the pointer goes over the button or the button takes the focus, so it follows the color of the button. A change of a class, such as the pressed class of a ToggleButton, does not draw it again: call MarkDirtyRepaint on the icon after the change. A USS background-image replaces the drawing.

The MediaIconDemo scene shows the thirteen kinds at any size and color, a replay bar whose buttons swap play and pause, volume and muted, and full size and restore, icons in buttons that take the hover and focus color, and four looks, one with game art in place of the drawing.

Credits for the sample clip

The sample clip VideoSample.mp4 of the VideoDemo and DirectorDemo examples 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 demos show this credit under the picture, and Third Party Notices.md holds the full notice. Keep the credit when you ship the clip, or play a clip of your own.

Styling

Class Element
tb-video-view The view: black, at least 240 by 160 px, with a focus ring when it has the focus.
tb-video-view--playing The view while it plays.
tb-video-view--preparing The view while the player opens the video.
tb-video-view--maximized The view at full screen.
tb-video-view__surface The Image with the picture.
tb-video-view__busy The spinning ProgressRing, 48 px in the middle. It shows only with --preparing.
tb-video-view__message The error text, white in the middle.
tb-video-view__bar The row of controls, in --tb-color-surface.
tb-video-view__button Each button of the bar: play, volume, loop, speed and full screen. A pressed toggle has the accent color.
tb-video-view__time The time and the length texts.
tb-video-view__seek The seek slider, or the WaveformBar that takes its place. In the bar, the waveform is 28 px high.
tb-video-view__volume-popup The popup of the volume button, above the button.
tb-video-view__volume The vertical volume slider in the popup, 110 px high.
tb-video-view__mute The mute Toggle under the slider.
tb-video-view__speed The speed button, with the speed as its text. Its menu is a ContextMenuComponent (tb-context-menu).
tb-media-icon An icon of the bar or of your own bar.
.my-cutscene .tb-video-view__bar {
    background-color: rgba(0, 0, 0, 0.6);
}
.my-cutscene .tb-video-view__button { color: var(--tb-color-accent); }

The tooltips "Play", "Pause", "Mute", "Volume", "Loop", "Playback speed" and "Full screen" are the ToolboxText keys tb.play, tb.pause, tb.mute, tb.volume, tb.loop, tb.playback-speed and tb.full-screen. See Localization.

The VideoDemo example scene has the bar; a bar of your own with round buttons, a sound button that opens a volume pill in an AnchoredPopup (a waveform, a level track with a music note in its fill, and a mute button with a crossed speaker while muted), and a ring for the time; a cutscene with Subtitles that follow the video and a hold to skip; and a video in a window.

The DirectorDemo scene uses the view as the clock of a small video editor: a Timeline under the video holds the titles, the captions, the effects and the volume changes, and the playhead follows the video. See Video director.