UI Image
Updated
An image element.
- Type
ent-ui-image- Script class
UIImageEntity- Family
- UI
Overview
Section titled “Overview”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.widthandheight: 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,borderColorandborderWidth: rounded corners and an outline.
Set it up
Section titled “Set it up”- Add a UI Image under a UI Panel or UI Layout. See Scenes and hierarchy.
- Set
srcto a project image or a URL. - Set
widthandheight, andobjectFit: 'cover'if every picture should fill the same box. - Add a Signal Emitter if a script should know when it loads, fails or is pressed.
Examples
Section titled “Examples”Thumbnails that change the main picture
Section titled “Thumbnails that change the main picture”Goal: pressing a small thumbnail shows its picture in a large image.
#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.entityIdsays which thumbnail was pressed, and itsprops.srcis copied to the main image.- A fixed
widthandheightwithobjectFit: 'cover'keep the main image the same size whatever each picture’s proportions.
A fallback when a picture fails
Section titled “A fallback when a picture fails”Goal: show a placeholder and a message when a remote picture cannot load.
#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
srcis set, so neither the firstreadynor the firsterroris missed. - On
UIImage::error, the script swaps in a local placeholder. The check onprops.srcstops it from retrying forever if the placeholder fails too. UIImage::readycarries the picture’swidthandheight.
Components
Section titled “Components”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- |
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" |
|
keep |
true |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
UIImage::pressUIImage::readyUIImage::error
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
A UI Image that steps through three pictures with Previous and Next buttons; the caption waits for the image's ready event and reports load errors.
Press Next and Previous to step through the pictures; the caption updates once each one has loaded. Drag outside the panel to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”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.
#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);}