Snap Zone
Updated
A box that snaps media into a neat grid.
- Type
ent-snap-zone- Script class
SnapZoneEntity- Family
- Interaction
Overview
Section titled “Overview”A Snap Zone is a box that pulls flat media into a neat grid when the player drops it nearby: a gallery wall where photos line up, a mood board, a shelf of documents, a slot where a video card must be placed. While the player drags an item the zone can take, the zone shows a glowing frame and a ghost of the slot the item would land in. Dropped close enough, the item snaps in: it moves under the zone, its rotation is squared up, and the grid lays it out. Dragged back out, it leaves the grid, which closes up, and returns to the parent it came from.
Its behaviour comes from the required Snap Zone component (SnapZoneComponent in scripts). The items it takes are media entities (image, KTX2, GIF, SVG, video, PDF, Lottie) that carry a Snappable component, which marks them as snappable, and a Grabbable, which lets the player drag them. Media entities placed as children of the zone in the saved scene are its items when the scene loads. Snapping happens in play only; in the editor the frame shows steady so you can place and size the zone.
Use it whenever players arrange media by hand. For dragging objects freely, a Grabbable alone is enough; for moving, turning and scaling with a gizmo, use a Pivotable.
The props that matter most:
capacity: the most items the zone holds, 1 to 256.columnsandgap: how many items go in a row before the next row starts, and the space between them, in metres. Each row and column takes the size of its largest item.widthandheight: the smallest size of the zone, which is the size of an empty zone. The bounds grow to wrap the items, withmarginaround them, and aredepththick.snap: how far past the bounds a dragged item is still pulled in, in metres.groups: the groups the zone takes. Empty takes any Snappable; otherwise it takes only Snappables that share a group with it. A Snappable with no groups snaps only into zones that list none.appearance:hint(the default) shows the frame only while an item it could take is dragged,framealways shows it,invisibledraws nothing of its own.color,highlightandanimationstyle the frame.
From a script, the component also exposes contents, the snapped entities in layout order, and size, the current bounds { width, height, depth }.
Set it up
Section titled “Set it up”- Add a Snap Zone entity to the scene and place it where the items should line up, facing the player: the grid is laid out on the zone’s local XY plane.
- Set
widthandheightto the empty size you want,columnsandgapfor the layout, andcapacityfor the number of items. - On each media entity that may snap in, add a Snappable and a Grabbable.
- To sort items between several zones, give the zones and the Snappables matching
groups, such asphotosanddocuments. - Choose an
appearance, and pickcolorandhighlightto suit the scene. - To react to items coming and going, add a Signal Emitter to the zone and set its
signal, for examplegallery.
Examples
Section titled “Examples”Count what is on the wall
Section titled “Count what is on the wall”Report how many items the gallery holds each time one is added or removed.
#pragma lifecycle(startup)
// Scene: a Snap Zone "Gallery" with a Signal Emitter on the signal 'gallery'.// Images around the room have a Snappable and a Grabbable.function startup() { const zone = findEntity('Gallery').getComponent(SnapZoneComponent); log('The gallery starts with', zone.contents.length, 'of', zone.props.capacity, 'items');
on('gallery', (event) => { if (event.eventType !== EventType.SnapZone.Change) return; const item = findEntity(event.data.entityId); const verb = event.data.snapped ? 'added' : 'removed'; log(item ? item.name : event.data.entityId, verb, '-', zone.contents.length, 'on the wall'); });}How it works:
zone.contentslists the snapped entities, including those placed in the zone in the saved scene;props.capacityis the limit.SnapZone::changefollows every snap and unsnap, withdata.entityIdnaming the item anddata.snappedsaying which happened.- Items already in the zone when the scene loads are not announced, which is why the script reads
contentsonce at startup.
Fill the board to open a door
Section titled “Fill the board to open a door”Lock each item in place once it snaps in, and announce when the board is full.
#pragma lifecycle(startup)
// Scene: a Snap Zone "Board" (capacity 4) with a Signal Emitter on the signal 'board'.// Four puzzle tiles (images) have a Snappable and a Grabbable.let zone;let solved = false;
function startup() { zone = findEntity('Board').getComponent(SnapZoneComponent);
on('board', (event) => { if (event.eventType !== EventType.SnapZone.Snap) return;
// Keep the tile on the board: it can no longer be dragged out. const tile = findEntity(event.data.entityId); const grab = tile && tile.getComponent(GrabbableComponent); if (grab) grab.setEnabled(false);
if (!solved && zone.contents.length >= zone.props.capacity) { solved = true; zone.setProps({ appearance: 'frame', color: '#00ffa3' }); emit('door/open'); } });}How it works:
SnapZone::snaparrives when an item is dropped in, withdata.entityId.- Disabling the tile’s Grabbable with
setEnabled(false)stops the player from dragging it out again. - When
contentsreachescapacity, the script switches the zone to an always-visible green frame withsetPropsand emitsdoor/openfor the door’s script, or for an Enable on Signal component, to react.
A screen slot that plays the video dropped in
Section titled “A screen slot that plays the video dropped in”Start a video when its card snaps into a screen slot, and stop it when it is taken out.
#pragma lifecycle(startup)
// Scene: a Snap Zone "Screen Slot" (capacity 1, groups ['videos']).// Video entities have a Snappable (groups ['videos']), a Grabbable,// and a Signal Emitter on the signal 'video/snap'.let slotId;
function startup() { slotId = findEntity('Screen Slot').entityId;
on('video/snap', (event) => { const snapped = event.eventType === EventType.Snappable.Snap; const unsnapped = event.eventType === EventType.Snappable.Unsnap; if ((!snapped && !unsnapped) || event.data.zoneId !== slotId) return;
const video = findEntity(event.entityId).getComponent(VideoComponent); if (snapped) video.play(); else video.stop(); });}How it works:
- The events come from the items this time: a Snappable sends
Snappable::snapandSnappable::unsnapon its own entity’s Signal Emitter, withdata.zoneId, the entity id of the zone. - Matching
groups(videoson both sides) means the slot takes only video cards, andcapacity: 1makes it hold one at a time. - The video’s own events (
Video::playand the rest) arrive on the same signal, so the script first keeps only the two Snappable events, then only those for this slot. event.entityIdis the video entity whose emitter sent the event, so the script plays exactly the card that was dropped in.
A custom backdrop that fits the zone
Section titled “A custom backdrop that fits the zone”Draw the zone with your own mesh, sized to the bounds as items arrive.
#pragma lifecycle(startup)
// Scene: a Snap Zone "Board" (appearance: invisible) with a Signal Emitter on 'board'.// As its child, a 1 × 1 plane mesh named "Backdrop" (no Snappable).function startup() { const zone = findEntity('Board').getComponent(SnapZoneComponent); const backdrop = findEntity('Backdrop'); const fit = (size) => backdrop.scale.set(size.width, size.height, 1);
fit(zone.size); on('board', (event) => { if (event.eventType === EventType.SnapZone.Resize) fit(event.data); });}How it works:
- With
appearance: 'invisible'the zone draws nothing of its own in play, but still snaps, lays out and sends its events. zone.sizegives the current bounds;SnapZone::resizeis sent whenever they change, its first layout included, with{ width, height, depth }as data.- The backdrop is a 1 × 1 plane, so scaling it by the width and height makes it cover the bounds exactly. It is a child without a Snappable, so the zone leaves it alone.
- Lowering
capacitynever removes items already in the zone; it only stops new ones from coming in. - Overlapping zones never share an item: the first zone to claim it wins. Space zones apart, or use
groupsto keep their items separate. - The layout runs only when an item comes, goes or changes size, never every frame. An
invisiblezone costs nothing to draw.
Components
Section titled “Components”What a new Snap Zone 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. |
| Snap Zone | comp- |
Required | One | A box that snaps media into a neat grid. |
You can also add 19 optional components, such as Animation Graph, Animation Timeline, Billboard, Box Collider, Capsule Collider, Character Controller.
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 |
|---|---|---|
appearance |
"hint" |
|
capacity |
6 |
1 ≤ x ≤ 256 |
groups |
[] |
|
columns |
3 |
1 ≤ x ≤ 256 |
gap |
0.05 |
0 ≤ x ≤ 10000 |
margin |
0.05 |
0 ≤ x ≤ 10000 |
depth |
0.1 |
0 ≤ x ≤ 10000 |
width |
1 |
0 ≤ x ≤ 10000 |
height |
1 |
0 ≤ x ≤ 10000 |
snap |
0.25 |
0 ≤ x ≤ 10000 |
color |
"#7a8cff" |
|
highlight |
"#00ffa3" |
|
animation |
"sweep" |
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.
SnapZone::snapSnapZone::unsnapSnapZone::changeSnapZone::resize
Like every entity, it also reports: 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.
An invisible Snap Zone wears a backdrop of its own: a one-by-one plane the script scales to the zone's size at startup and on every SnapZone::resize, as items fill the grid.
Drag the pictures onto the backdrop: it grows as the grid fills, and shrinks when you take them off. Drag elsewhere to orbit the camera, 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 Snap Zone.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Snap Zone'); if (!(entity instanceof SnapZoneEntity)) return;
// Its Snap Zone component holds the settings listed under Props. const component = entity.getComponent(SnapZoneComponent); log('capacity is', component.props.capacity); component.setProps({ capacity: 9 });}