An image gallery
Updated
A UI Image steps through three pictures with Previous and Next; the caption waits for the image’s ready event and reports load errors.
Press Next and Previous to step through the pictures. Drag outside the panel to orbit the camera, scroll to zoom.
An image gallery
#pragma lifecycle(startup)
// Scene: a UI Panel with a UI Image named 'Photo' (Signal Emitter: 'gallery:photo'),// a UI Label named 'Caption', and two UI Buttons named 'Previous' and 'Next'// (Signal Emitters: 'gallery:prev' and 'gallery:next').const PHOTOS = [ { src: '/example-assets/lemon-decal.png', caption: 'Lemon slice' }, { src: '/example-assets/bang.png', caption: 'Comic bang' }, { src: '/example-assets/alpha.png', caption: 'Star burst' },];let index = 0;let photo;let caption;
function show() { photo.setProps({ src: PHOTOS[index].src }); caption.setProps({ text: 'Loading…' });}
function startup() { photo = findEntity('Photo').getComponent(UIImageComponent); caption = findEntity('Caption').getComponent(UILabelComponent); photo.setProps({ width: 320, height: 200, objectFit: 'cover', borderRadius: 12 });
on('gallery:photo', (event) => { if (event.eventType === EventType.UIImage.Ready) caption.setProps({ text: PHOTOS[index].caption }); if (event.eventType === EventType.UIImage.Error) caption.setProps({ text: 'Could not load this photo.' }); }); on('gallery:prev', (event) => { if (event.eventType !== EventType.UIButton.Press) return; index = (index + PHOTOS.length - 1) % PHOTOS.length; show(); }); on('gallery:next', (event) => { if (event.eventType !== EventType.UIButton.Press) return; index = (index + 1) % PHOTOS.length; show(); });
show();}How it works
Section titled “How it works”The script is explained step by step in An image gallery.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
ImageGallery.xrproj