Skip to content

UI Image

Updated

An image element.

Type
ent-ui-image
Script class
UIImageEntity
Family
UI

A UI Image shows a picture inside a UI: an item icon, a photo, a map, a logo. Its UI Image component (comp-ui-image) loads the picture from src, a URL or one of the project’s image assets, and fits it into its box. It can round the corners and draw an outline.

An image sits nested in a panel or layout, or stands alone as a world-space root, like a small picture frame. With both sizes automatic, it uses the picture’s natural size and scales down to fit the space it has; with one size set, it keeps the picture’s proportions.

With a Signal Emitter on its entity, an image reports UIImage::ready { width, height } when it shows, UIImage::error { message } when it cannot load, and UIImage::press {} when it is pressed. With a Link component, a press opens a web page instead.

Use a UI Image for pictures inside a UI. For a round profile picture with a status dot, use a UI Avatar. For a picture placed in the 3D world on its own, use the world-space Image entity.

The props that matter most:

  • src: the picture.
  • width and height: the size in layout pixels (px).
  • objectFit: 'cover' (the default) crops the picture to fill the box; 'fill' stretches it.
  • keepAspectRatio: keep the proportions when only one size is set (on by default).
  • borderRadius, borderColor and borderWidth: rounded corners and an outline.
  1. Add a UI Image under a UI Panel or UI Layout. See Scenes and hierarchy.
  2. Set src to a project image or a URL.
  3. Set width and height, and objectFit: 'cover' if every picture should fill the same box.
  4. Add a Signal Emitter if a script should know when it loads, fails or is pressed.

Goal: pressing a small thumbnail shows its picture in a large image.

Thumbnails.jx
#pragma lifecycle(startup)
// Scene: a UI Image named 'Main Photo', and a row of UI Images named 'Thumb 1', 'Thumb 2'
// and 'Thumb 3', each with a picture in src and a Signal Emitter on 'gallery:thumb'.
function startup() {
const main = findEntity('Main Photo').getComponent(UIImageComponent);
main.setProps({ width: 360, height: 240, objectFit: 'cover', borderRadius: 12 });
on('gallery:thumb', (event) => {
if (event.eventType !== EventType.UIImage.Press) return;
const thumb = findEntity(event.entityId).getComponent(UIImageComponent);
main.setProps({ src: thumb.props.src });
});
}

How it works:

  • Each thumbnail’s Signal Emitter makes it pressable; they share one signal, so one listener handles all of them.
  • event.entityId says which thumbnail was pressed, and its props.src is copied to the main image.
  • A fixed width and height with objectFit: 'cover' keep the main image the same size whatever each picture’s proportions.

Goal: show a placeholder and a message when a remote picture cannot load.

PosterFallback.jx
#pragma lifecycle(startup)
// Scene: a UI Image named 'Poster' (Signal Emitter: 'poster') and a UI Label named 'Poster Caption'.
const REMOTE = 'https://example.com/posters/tonight.jpg';
const FALLBACK = '/assets/posters/placeholder.png';
function startup() {
const poster = findEntity('Poster').getComponent(UIImageComponent);
const caption = findEntity('Poster Caption').getComponent(UILabelComponent);
on('poster', (event) => {
if (event.eventType === EventType.UIImage.Ready) {
caption.setProps({ text: `Tonight's show (${event.data.width}×${event.data.height})` });
}
if (event.eventType === EventType.UIImage.Error && poster.props.src !== FALLBACK) {
caption.setProps({ text: 'Poster unavailable' });
poster.setProps({ src: FALLBACK });
}
});
caption.setProps({ text: 'Loading…' });
poster.setProps({ src: REMOTE });
}

How it works:

  • The listener is registered before src is set, so neither the first ready nor the first error is missed.
  • On UIImage::error, the script swaps in a local placeholder. The check on props.src stops it from retrying forever if the placeholder fails too.
  • UIImage::ready carries the picture’s width and height.

What a new UI Image is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.

Component Type Role Per entity What it does
Layer comp-layer Required One Puts the entity on a render/raycast layer so cameras and rays can include or skip it.
Lock comp-lock Required One Editor-only flag that stops the entity from being selected or moved.
Enable comp-enable Required One Turns the entity and its children on or off: hides them and stops their behavior.
Transform comp-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
UI Image comp-ui-image Required One An image element.

You can also add 3 optional components, such as Attribution, Link, Signal Emitter.

The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).

Prop Default Range
objectFit "cover"
keepAspectRatio true

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

  • UIImage::press
  • UIImage::ready
  • UIImage::error

Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My UI Image.

UIImageEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My UI Image');
if (!(entity instanceof UIImageEntity)) return;
// Its UI Image component holds the settings listed under Props.
const component = entity.getComponent(UIImageComponent);
log(component.props);
}