Skip to content

UI Label

Updated

A text label.

Type
ent-ui-label
Script class
UILabelEntity
Family
UI

A UI Label draws text: a title, a score, a caption, a paragraph of instructions, a name tag over an object. Its UI Label component (comp-ui-label) holds the text and styles it, and the text stays sharp at any distance and any size, on a screen or in a headset.

The text sits in a box you style with familiar CSS-like props: padding inside the box, a backgroundColor, rounded corners with borderRadius, and an outline with borderColor. A label is usually nested in a panel or layout, where it resizes the layout around its text. It can also be a root of its own: placed under a 3D object and raised above it, with a Billboard so it always faces the camera, it becomes a floating tag that follows the object.

A label is passive by default and takes no pointer. With a Signal Emitter on its entity it becomes pressable and sends UILabel::press; with a Link component, a press opens a web page instead.

Use a UI Label for text inside a UI. For text that is part of the 3D world, such as a sign painted on a wall, use the world-space Text entity. For text players type, use a UI Input.

The props that matter most:

  • text: what it shows. A new label shows Text.
  • fontSize, fontWeight and font: the size in layout pixels (px), the weight ('light' to 'bold') and the font family by name.
  • color and textAlign: the text colour and its alignment in a box wider than the text.
  • maxWidth (or width): the width the text wraps at. Without one, the label is a single line however long its text.
  • padding, backgroundColor and borderRadius: the box around the text.
  1. Add a UI Label directly under a UI Panel or UI Layout, where it should appear in the order. See Scenes and hierarchy.
  2. Set its text, then fontSize and color.
  3. For several lines, set a maxWidth so it wraps.
  4. For a floating tag, place the label under the object instead, raise it above the object with its position, and add a Billboard component.
  5. To make it pressable, add a Signal Emitter with a signal.

Goal: count down from 60 seconds, turn red for the last ten, and announce the end.

Countdown.jx
#pragma lifecycle(startup, update)
// Scene: a UI Label named 'Timer' in a HUD panel.
let remaining = 60;
let label;
function startup() {
label = findEntity('Timer').getComponent(UILabelComponent);
label.setProps({ fontSize: 32, fontWeight: 'bold', font: 'jetbrains-mono' });
}
function update(dt) {
if (remaining <= 0) return;
const before = Math.ceil(remaining);
remaining = Math.max(0, remaining - dt);
const seconds = Math.ceil(remaining);
if (seconds === before) return;
label.setProps({ text: `0:${String(seconds).padStart(2, '0')}`, color: seconds <= 10 ? '#f87171' : '#ffffff' });
if (seconds === 0) emit('game:time-up');
}

How it works:

  • #pragma lifecycle(startup, update) asks for both hooks: startup runs once, update(dt) every frame with dt, the seconds since the last frame.
  • The label is changed only when the shown second changes, not on every frame.
  • The monospace jetbrains-mono family keeps every digit the same width, so the text does not jitter as it counts.
  • emit('game:time-up') tells other scripts that time ran out.

Goal: float a styled label above a shop item that always turns to face the player.

PriceTag.jx
#pragma lifecycle(startup)
// Scene: a 3D object named 'Lantern'. The script pins a tag 0.5 m above it.
function startup() {
const lantern = findEntity('Lantern');
const tag = new UILabelEntity();
tag.getComponent(UILabelComponent).setProps({
text: 'Lantern · 12 coins',
fontSize: 32,
padding: [8, 14],
color: '#fef3c7',
backgroundColor: 'rgba(28, 25, 23, 0.9)',
borderRadius: 12,
borderColor: '#f59e0b',
});
tag.position.set(0, 0.5, 0);
tag.addComponent(new BillboardComponent());
lantern.add(tag);
}

How it works:

  • Added under the lantern, the label is a root of its own: its position places it 0.5 m above the lantern’s origin, and it moves with the lantern.
  • The Billboard turns it toward the camera, so it reads from any side.
  • padding: [8, 14] puts 8 px above and below the text and 14 px on the sides; the border is drawn because borderColor is set.

Goal: show a short teaser, and the full hint once the player presses it.

Hint.jx
#pragma lifecycle(startup)
// Scene: a UI Panel holding a UI Label named 'Hint'
// with a Signal Emitter whose signal is 'puzzle:hint'.
const FULL = 'The third lever opens the gate, but only after the bell rings twice.';
let revealed = false;
function startup() {
const hint = findEntity('Hint').getComponent(UILabelComponent);
hint.setProps({ text: 'Stuck? Press for a hint.', color: '#93c5fd', maxWidth: 280 });
on('puzzle:hint', (event) => {
if (event.eventType !== EventType.UILabel.Press || revealed) return;
revealed = true;
hint.setProps({ text: FULL, color: '#ffffff' });
});
}

How it works:

  • The Signal Emitter makes the label pressable; each press arrives as UILabel::press.
  • maxWidth: 280 makes the long hint wrap onto several lines instead of stretching the panel.
  • The revealed flag ignores later presses.

What a new UI Label 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 Label comp-ui-label Required One A text label.

You can also add 6 optional components, such as Billboard, Enable On Signal, Link, Networkable, Networkable Trait, 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
text "Text"
fontWeight "normal" 1 ≤ x ≤ 1000
padding 1
textAlign "left"
lineHeight 1.2 0 ≤ x ≤ 10
letterSpacing 0 -16384 ≤ x ≤ 16384

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

  • UILabel::press

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 Label.

UILabelEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My UI Label');
if (!(entity instanceof UILabelEntity)) return;
// Its UI Label component holds the settings listed under Props.
const component = entity.getComponent(UILabelComponent);
log('padding is', component.props.padding);
component.setProps({ padding: 1.5 });
}