Text
Updated
Crisp, cheap flat text label in the world.
- Type
ent-text- Script class
TextEntity- Family
- Media
Overview
Section titled “Overview”A Text entity draws flat words in the world: a sign, a name tag, a caption, a score or a timer. Its letters are drawn with a technique that keeps edges sharp at any distance, and changing the string only re-lays out the glyphs, so it is cheap enough to update every second. The text is unlit and looks the same in the editor and in play.
Pick a sibling when it fits better: Text 3D for short titles and logos with depth that catch light and cast shadows, a UI Label for text inside a UI panel, or an Image SVG for a fixed logo drawing.
The props that matter most, on its Text component (TextComponent in scripts):
text: the string drawn.fontSize: the letter size in metres. The default, 1, is large; signs usually use 0.1 to 0.3.color,outlineWidthandoutlineColor: the fill colour and an outline that keeps text readable on busy backgrounds.outlineWidthtakes metres or a percentage of the font size, such as'5%'.fontandfontWeight: a font family by name, such asinter,roboto,nunito,montserratorjetbrains-mono, and its weight, such asbold.maxWidthandtextAlign: wrap lines at a width in metres, and align themleft,right,centerorjustify.anchorXandanchorY: which point of the text sits at the entity’s position, such ascenterandmiddle.
Set it up
Section titled “Set it up”- Add a Text entity to the scene and name it, such as
Score. - Type its
text, and setfontSizeto a size that reads well from where the player stands. - Pick a
font,fontWeightandcolor. Add an outline if the background is busy. - For a paragraph, set
maxWidthso lines wrap, andtextAlignto align them. - Use
anchorXandanchorYto decide which part of the text the entity’s position marks.
Examples
Section titled “Examples”A score counter
Section titled “A score counter”Show the score and update it each time a point is scored.
#pragma lifecycle(startup)
// Scene: a Text entity "Score". Other scripts emit 'point'.let score = 0;let label;
function startup() { label = findEntity('Score').getComponent(TextComponent); label.setProps({ text: 'Score: 0' });
on('point', () => { score += 1; label.setProps({ text: `Score: ${score}` }); });}How it works:
getComponent(TextComponent)reaches the text’s settings.setProps({ text })replaces the string; only the glyph layout runs again, so frequent updates are cheap.
A countdown timer
Section titled “A countdown timer”Count down from 60 seconds and turn the text red for the last ten.
#pragma lifecycle(startup, update)
// Scene: a Text entity "Timer".let label;let remaining = 60;let shown = -1;
function startup() { label = findEntity('Timer').getComponent(TextComponent);}
function update(dt) { if (remaining <= 0) return; remaining = Math.max(0, remaining - dt); const seconds = Math.ceil(remaining); if (seconds === shown) return; shown = seconds; label.setProps({ text: `${seconds}`, color: seconds <= 10 ? '#ef4444' : '#ffffff' }); if (seconds === 0) emit('timer/done');}How it works:
update(dt)receivesdt, the seconds since the last frame; subtracting it each frame counts down in real time.- The script calls
setPropsonly when the whole-second value changes, not every frame. - At zero it raises
timer/donefor any other script to react.
A name tag that fits its text
Section titled “A name tag that fits its text”Set a long name on a tag and size a background panel to the text once it is laid out.
#pragma lifecycle(startup)
// Scene: a Text entity "Name" with anchorX: center, and a box mesh// "Name Background" behind it.async function startup() { const nameEntity = findEntity('Name'); const text = nameEntity.getComponent(TextComponent); text.setProps({ text: 'Dr. Alexandra Moreau', fontSize: 0.12, outlineWidth: '4%' });
await text.sync();
const size = new THREE.Box3().setFromObject(nameEntity).getSize(new THREE.Vector3()); findEntity('Name Background').setScale(size.x + 0.1, size.y + 0.06, 0.01);}How it works:
- Glyph layout runs in the background, so a new string shows a moment later.
await text.sync()waits for it inside theasyncstartuphook. - After
sync(), the entity’s bounds match the new text;THREE.Box3measures them. - The background is scaled to the text plus a margin, so any name fits.
- Measure text only after
await component.sync(); reading its bounds right aftersetPropsgives the old size. - A font loads the first time a text uses it, and the text draws nothing until then, so it never flashes in another font. A family the project does not have draws the default font and warns once.
- Text entities do not accept a Billboard. To keep a label facing the player, turn it from a script.
Components
Section titled “Components”What a new Text 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. |
| Text | comp-text |
Required | One | Crisp, cheap flat text label in the world. |
You can also add 15 optional components, such as Animation Graph, Animation Timeline, Attribution, Enable On Signal, Follow Target, Grabbable.
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" |
|
fontSize |
1 |
0. |
color |
"#ffffff" |
|
font |
"inter" |
|
lineHeight |
1 |
-10 ≤ x ≤ 10 |
letterSpacing |
0 |
-10 ≤ x ≤ 10 |
textAlign |
"left" |
|
anchorX |
"center" |
|
anchorY |
"middle" |
|
outlineWidth |
0 |
0 ≤ x ≤ 100 |
outlineColor |
"#000000" |
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.
It reports the events every entity has: 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 name tag over a character waits for its text layout, measures it to fit a dark backing plate, and turns to the camera every frame.
Drag to orbit the camera: the name tag keeps facing you. 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 Text.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Text'); if (!(entity instanceof TextEntity)) return;
// Its Text component holds the settings listed under Props. const component = entity.getComponent(TextComponent); log('fontSize is', component.props.fontSize); component.setProps({ fontSize: 1.5 });}