Class ImageLightbox
A modal viewer for a single image, sized to the panel.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class ImageLightbox : BaseVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder
Remarks
The image is never scaled beyond its natural size: an asset whose only available file is small stays crisp at its own resolution rather than being upscaled into a blurry wall. The aspect ratio is preserved by construction, because the drawn size is the natural size times a single uniform factor that is only ever a shrink.
Focus trapping, Esc, the scrim and outside-click dismissal all come from Modal rather than a hand-rolled overlay: Show(VisualElement) wraps this element in one and returns it, so the caller can keep configuring or dismiss it.
Why it measures. The registry sizes this in viewport units — max-w-[min(90vw,64rem)],
max-h-[90vh], the image at max-h-[70vh] — and UI Toolkit has no viewport unit. Nor is a
percentage usable here: the lightbox's parent is the modal's shrink-to-fit content box, whose own
size is what the lightbox is trying to determine, so a percentage against it is circular. So the
element measures the Panel it is shown in on GeometryChangedEvent and applies the
same fractions itself. They are viewport fractions, not design tokens, which is why they live in
C# as panelWidthFraction / panelHeightFraction /
imageHeightFraction rather than being invented as --appui-* vars. Everything that
is design — the surface, the padding, the caption's colour and type — stays in USS on tokens.
The caption doubles as the viewer's title, so there is no separate heading repeating the same string. With no src, or one whose texture never resolved, the fallback element is shown in the image's place.
Navigation. Set navigationCount above 1 to show the image as one of a set: previous and next buttons either side of the caption, Left / Right arrow keys step too, and the caption leads with the position ("2 / 5"). Stepping never wraps. Each step updates navigationIndex and raises navigationIndexChanged; the caller swaps src and caption. When a step reaches an end and disables the button that had focus, focus moves to the other step button so the arrow keys keep working.
Tall images. A picture more than tallRatio times taller than wide is shown at its full (capped) width and scrolls vertically, rather than being shrunk to the panel's height until nothing in it reads.
Examples
Opening a viewer over the current panel.
var lightbox = new ImageLightbox
{
src = BackgroundExtensions.FromObject(fullResTexture),
alt = "Cliff face, tiling",
caption = "cliff-face-01.png",
};
lightbox.Show(rootVisualElement);
UXML declaration — usually you let ZoomableThumbnail own one instead.
<appui:ImageLightbox caption="cliff-face-01.png" alt="Cliff face, tiling" />
Constructors
ImageLightbox()
Default constructor.
Declaration
public ImageLightbox()
Fields
captionUssClassName
The styling class of the caption under the image.
Declaration
public const string captionUssClassName = "appui-imagelightbox__caption"
Field Value
| Type | Description |
|---|---|
| string |
fallbackUssClassName
The styling class of the container holding the fallback content.
Declaration
public const string fallbackUssClassName = "appui-imagelightbox__fallback"
Field Value
| Type | Description |
|---|---|
| string |
footerUssClassName
The styling class of the row holding the step buttons and the title.
Declaration
public const string footerUssClassName = "appui-imagelightbox__footer"
Field Value
| Type | Description |
|---|---|
| string |
imageHeightFraction
The share of the panel height the image itself may occupy, leaving room for the caption.
The registry's max-h-[70vh].
Declaration
public const float imageHeightFraction = 0.7
Field Value
| Type | Description |
|---|---|
| float |
imageUssClassName
The styling class of the element the image is painted on.
Declaration
public const string imageUssClassName = "appui-imagelightbox__image"
Field Value
| Type | Description |
|---|---|
| string |
navigationUssClassName
The modifier class set while the viewer shows one image of a set.
Declaration
public const string navigationUssClassName = "appui-imagelightbox--navigation"
Field Value
| Type | Description |
|---|---|
| string |
nextUssClassName
The styling class of the next image button.
Declaration
public const string nextUssClassName = "appui-imagelightbox__next"
Field Value
| Type | Description |
|---|---|
| string |
panelHeightFraction
The share of the panel height the viewer may occupy. The registry's 90vh.
Declaration
public const float panelHeightFraction = 0.9
Field Value
| Type | Description |
|---|---|
| float |
panelWidthFraction
The share of the panel width the viewer may occupy. The registry's 90vw.
Declaration
public const float panelWidthFraction = 0.9
Field Value
| Type | Description |
|---|---|
| float |
positionUssClassName
The styling class of the position indicator ("2 / 5").
Declaration
public const string positionUssClassName = "appui-imagelightbox__position"
Field Value
| Type | Description |
|---|---|
| string |
previousUssClassName
The styling class of the previous image button.
Declaration
public const string previousUssClassName = "appui-imagelightbox__previous"
Field Value
| Type | Description |
|---|---|
| string |
scrollUssClassName
The styling class of the scroller holding the image. It only scrolls for a tall image.
Declaration
public const string scrollUssClassName = "appui-imagelightbox__scroll"
Field Value
| Type | Description |
|---|---|
| string |
separatorUssClassName
The styling class of the separator between the position and the caption.
Declaration
public const string separatorUssClassName = "appui-imagelightbox__separator"
Field Value
| Type | Description |
|---|---|
| string |
tallRatio
Taller than wide by more than this, a picture scrolls at full width instead of shrinking.
The registry's TALL_RATIO.
Declaration
public const float tallRatio = 1.25
Field Value
| Type | Description |
|---|---|
| float |
tallUssClassName
The modifier class set while the image is tall enough to scroll.
Declaration
public const string tallUssClassName = "appui-imagelightbox--tall"
Field Value
| Type | Description |
|---|---|
| string |
titleUssClassName
The styling class of the title, the position followed by the caption.
Declaration
public const string titleUssClassName = "appui-imagelightbox__title"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The ImageLightbox main styling class.
Declaration
public const string ussClassName = "appui-imagelightbox"
Field Value
| Type | Description |
|---|---|
| string |
Properties
alt
A textual description of the image, for assistive technology and for the tooltip.
Declaration
[CreateProperty]
[UxmlAttribute("alt")]
[Header("Image Lightbox")]
public string alt { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
caption
Shown under the image and used as the viewer's accessible name.
Declaration
[CreateProperty]
[UxmlAttribute("caption")]
public string caption { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
fallback
Rendered in place of the image when there is no src, or its texture never resolved.
Declaration
public VisualElement fallback { get; set; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
hasImage
Whether there is an image to paint.
Declaration
public bool hasImage { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
A VectorImage counts, even though NaturalSize() deliberately reports zero for one: it has no pixel resolution to cap against, which is a sizing question, not a question of whether an image is there. Asking NaturalSize alone would hide a perfectly good vector image behind the fallback.
isTall
Whether the image is tall enough (see tallRatio) to be shown at full width and scrolled.
Declaration
public bool isTall { get; }
Property Value
| Type | Description |
|---|---|
| bool |
navigationCount
How many images the set holds. Above 1, the viewer shows step buttons, the position and answers the arrow keys; at 0 or 1 it shows a single image.
Declaration
[CreateProperty]
[UxmlAttribute("navigation-count")]
public int navigationCount { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
navigationIndex
Which image of the set is shown, counting from zero. Only meaningful while navigationCount is above 1.
Declaration
[CreateProperty]
[UxmlAttribute("navigation-index")]
public int navigationIndex { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
nextLabel
The accessible name of the next image button.
Declaration
[CreateProperty]
[UxmlAttribute("next-label")]
public string nextLabel { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
previousLabel
The accessible name of the previous image button.
Declaration
[CreateProperty]
[UxmlAttribute("previous-label")]
public string previousLabel { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
src
The image to display. Leave it unset while a larger image is still resolving, or when none exists — the fallback stands in.
Declaration
[CreateProperty]
public Background src { get; set; }
Property Value
| Type | Description |
|---|---|
| Background |
Methods
Show(VisualElement)
Show this viewer as a modal over the given panel.
Declaration
public Modal Show(VisualElement referenceView)
Parameters
| Type | Name | Description |
|---|---|---|
| VisualElement | referenceView | Any element in the panel the viewer should cover. |
Returns
| Type | Description |
|---|---|
| Modal | The modal hosting this viewer. |
Remarks
The modal owns the scrim, the focus trap, Esc and outside-click dismissal. The returned instance is not yet shown, so the caller can keep configuring it; call Show() on it, which this method already does, or Dismiss() to close it again.
Events
navigationIndexChanged
Raised when the user steps to another image, with the index to show. The caller swaps src and caption.
Declaration
public event Action<int> navigationIndexChanged
Event Type
| Type | Description |
|---|---|
| Action<int> |