A poster that changes on click
Updated
Your first media script: clicking an Image shows the next picture, and a Text caption says “Loading…” until the Image reports ready, then the picture’s title.
Click the poster to show the next picture.
A poster that changes on click
#pragma lifecycle(startup)
const POSTERS = [ { src: '/example-assets/lemon-decal.png', title: 'Pink Lemon' }, { src: '/example-assets/bang.png', title: 'Bang!' }, { src: '/example-assets/alpha.png', title: 'Starlight' },];
let poster;let caption;let index = 0;
function show(i) { index = i % POSTERS.length; if (poster.props.src === POSTERS[index].src) { caption.setProps({ text: POSTERS[index].title }); // already showing return; } caption.setProps({ text: 'Loading…' }); poster.setProps({ src: POSTERS[index].src });}
function startup() { poster = findEntity('Poster').getComponent(ImageComponent); caption = findEntity('Caption').getComponent(TextComponent);
poster.setProps({ sizing: 'auto', width: 1.6, height: 1.6 }); caption.setProps({ fontSize: 0.1, anchorY: 'top' }); show(0);
on('poster', (event) => { if (event.eventType === EventType.Selectable.Select) { findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again show(index + 1); } if (event.eventType === EventType.Image.Ready) caption.setProps({ text: POSTERS[index].title }); if (event.eventType === EventType.Image.Error) caption.setProps({ text: 'Picture unavailable' }); });}How it works
Section titled “How it works”The script is explained step by step in A poster that changes on click.
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.
FirstMedia.xrproj