A shop button with a badge
Updated
A Buy button shows its price in its text and how many torches you own in its badge, and is disabled until you have enough coins.
Press Pick up a coin five times to afford a torch, then press Buy. Drag outside the panel to orbit the camera, scroll to zoom.
A shop button with a badge
#pragma lifecycle(startup)
// Scene: a UI Button named 'Buy' (Signal Emitter: 'shop/buy')// and a UI Label named 'Coins'. Other scripts emit 'coin' when the player picks one up.const PRICE = 5;let coins = 0;let owned = 0;
function startup() { const buy = findEntity('Buy').getComponent(UIButtonComponent); const label = findEntity('Coins').getComponent(UILabelComponent);
function refresh() { label.setProps({ text: `Coins: ${coins}` }); buy.setText(`Buy torch (${PRICE})`); buy.setBadge(owned); buy.setDisabled(coins < PRICE); }
on('coin', () => { coins += 1; refresh(); });
on('shop/buy', (event) => { if (event.eventType !== EventType.UIButton.Press) return; coins -= PRICE; owned += 1; refresh(); });
refresh();}How it works
Section titled “How it works”The script is explained step by step in A shop button with a badge.
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.
ShopButton.xrproj