Skip to main content

Editor scripts

An editor script adds your own tools to the editor, like Unity's Editor scripts:

  • Menu commands in ☰ → Tools: plant 50 trees in a circle, line up the selected objects, rename a row of objects 1, 2, 3…
  • Buttons on the Inspector: "Rebuild path" on every object with your Patrol script.
  • Gizmos: lines in the scene view, such as each guard's patrol circle or a spawner's area.
  • Objects that rebuild while you edit (Unity's OnValidate): a fence that adds posts when you make it longer.
  • Custom Inspectors: your own sliders, fields and buttons on a script's card.
  • Import rules (Unity's AssetPostprocessor): put every new sound in a Sounds folder, give UI pictures a 9-slice border.
  • Windows (Unity's EditorWindow): your own window of controls, docked anywhere on a computer.

Editor scripts run only in the editor. They are never part of the game, a published link or a build.

An editor script's commands in ☰ → Tools
An editor script's commands in ☰ → Tools
Its button on the Patrol card, and its gizmo: the patrol circle
Its button on the Patrol card, and its gizmo: the patrol circle

Make one​

  1. Logic → Code scripts → + New → Editor script (or Files → New script).
  2. It starts with an example command. Change it, and it is in ☰ → Tools straight away.

On a computer, the commands are also in the Tools menu at the top.

editor.menu('Level/Scatter trees', async () => {
const n = await editor.promptNumber('How many?', 20);
if (n == null) return; // cancelled
for (let i = 0; i < n; i++) {
const a = (i / n) * Math.PI * 2;
editor.scene.add('Tree', { shape: 'cylinder', position: [Math.cos(a) * 8, 0.5, Math.sin(a) * 8], color: '#2e7d32' });
}
editor.toast('Planted ' + n);
});
  • The part before the last / groups the command ("Level") and is shown beside it.
  • Everything a command changes is one undo step: one Undo takes back all 20 trees.
  • If it fails, nothing changes, and the error shows at the bottom with its line.
  • A command that runs longer than 10 seconds (an endless loop) is stopped. Time spent waiting for your answer to a question doesn't count.

Buttons on the Inspector​

editor.button('Patrol', 'Bigger patrol', (e) => {
e.script('Patrol').radius += 1;
});

The first value says which objects get the button:

TargetThe button shows…
An object script's name ('Patrol')on that script's card
A component type ('light', 'particles'…)on that component's card
An object name ('Door') or a tag ('#enemy')under the object's components

e is the object the Inspector shows.

Gizmos​

editor.gizmos('Patrol', (e, draw, selected) => {
draw.color(selected ? '#ffcc00' : '#999999')
.circle([0, 0, 0], e.script('Patrol').radius);
});

The function runs for every object matching the target (same targets as buttons), and again whenever the project or the selection changes.

Draw
draw.color('#ff8800')colour for what comes next
draw.line(from, to)a line
draw.ray(from, direction)a line from a point along a direction
draw.circle(center, radius)a flat circle on the ground plane
draw.sphere(center, radius)a wire sphere
draw.box(center, size)a wire box
draw.path(points, closed?)lines through the points
draw.world()from here on, points are world positions

Points are [x, y, z]. They are in the object's own space: they move and turn with the object, but its scale doesn't stretch them, so a radius of 5 is 5 m. Turn them off in the scene view's Gizmos menu → Editor script gizmos.

Objects that rebuild while you edit​

editor.onValidate('Fence', (e) => {
const f = e.script('Fence');
for (const c of e.children) c.destroy();
for (let i = 0; i < f.length; i++) {
editor.scene.add('Post', { shape: 'box', parent: e, position: [i * f.gap, 0.5, 0], scale: [0.2, 1, 0.2] });
}
});

It runs right after you change a matching object (same targets as buttons): a field on the Inspector, moving it, renaming it.

  • What it changes joins your edit's undo step: one Undo takes back the new length and the posts it made.
  • It runs only for the objects you changed, not for every match in the scene.
  • Undo and Redo don't run it; they put back what was there.
  • It must finish within 2 seconds. If it fails, nothing it did is kept, and the error shows with its line.

Custom Inspectors​

A custom Inspector on the Fence script: the slider sets the length, onValidate builds the posts
A custom Inspector on the Fence script: the slider sets the length, onValidate builds the posts
editor.inspector('Fence', (e, ui) => {
const f = e.script('Fence');
ui.header('Fence');
f.length = ui.slider('Length', f.length, 1, 12, 1);
f.gap = ui.number('Gap', f.gap, { min: 0.2 });
ui.label('Posts', e.children.length);
if (ui.button('Clear')) f.length = 1;
}, { replace: true });

It works like Unity's OnInspectorGUI: each control shows a value and returns it, and the new value when you have just changed it, so you write it straight back. The function runs again after every change to draw the new state.

  • It shows on the card of the object script or component the target names, or under the components for an object name or #tag.
  • { replace: true } hides the card's usual fields. Leave it out to add your section under them.
  • Each change is one undo step; dragging a slider is one step, not one per frame.
ControlReturns
ui.header(text), ui.label(text, value?), ui.space()nothing
ui.number(label, value, { min, max, step })a number
ui.int(label, value, { min, max })a whole number
ui.slider(label, value, min, max, step?)a number
ui.toggle(label, value)true / false
ui.text(label, value), ui.color(label, '#ff8800')text
ui.select(label, value, ['Low', 'High'])the chosen option
ui.vector(label, [x, y, z])[x, y, z]
ui.button(label)true when just pressed
ui.row(() => { … })puts the controls inside side by side

Windows​

editor.window('Level stats', (ui) => {
ui.header('Scene');
ui.label('Objects', editor.scene.objects.length);
ui.label('Selected', editor.selection.map((e) => e.name).join(', ') || '-');
if (ui.button('Add crate')) editor.scene.add('Crate', { shape: 'box' });
});

A window of your own, drawn with the same controls as a custom Inspector, with no object. Open it from Window in the menu bar, or ☰ → Tools on a phone.

  • On a computer it is a window like the editor's own: drag its tab anywhere (see Windows and layout). On a phone it opens full screen.
  • It is drawn again when the project or the selection changes, and after each control you use.
  • What a control changes is one undo step.

Import rules​

editor.onImport((file) => {
if (file.kind === 'audio') file.folder = 'Sounds';
if (file.kind === 'texture' && file.name.startsWith('ui_')) { file.folder = 'UI'; file.sliceBorder = [12, 12, 12, 12]; }
});

It runs once for each file added to the project, however it was added: Import, the libraries, the sprite editor, the sound maker. What it changes joins the import's undo step.

file.
name, folderchange them
kind'texture', 'model', 'audio', 'font' or 'video'
mime, sizethe file type and its size in bytes
sliceBorderpictures: the 9-slice border [left, right, top, bottom] in pixels
spritepictures: the sprite sheet (frameW, frameH, frames, fps)
bpmsounds: the tempo

The project's files are also in editor.assets, and editor.asset('name') finds one.

What an editor script can use​

editor.scene.objects // every object of the open scene
editor.scene.find('Guard') // by name or id
editor.scene.findAll('#enemy') // by name, or tag with #
editor.scene.add('Rock', { shape: 'sphere', position: [0, 1, 0], scale: [2, 2, 2], color: '#888', parent: 'Rocks' })
editor.scene.place('Coin', [3, 1, 0]) // a prefab
editor.selection // the selected objects
editor.select(list) // select others
editor.scenes, editor.prefabs // names
editor.assets, editor.asset('hit') // the project's files

e.name, e.position, e.rotation, e.scale // e.position.x += 1, or e.position = [0, 1, 0]
e.visible, e.enabled, e.layer, e.tags, e.hasTag('enemy')
e.parent = other // keeps its place in the world
e.children
e.component('light').intensity = 2 // any component's settings
e.addComponent('rigidbody', { mass: 3 }), e.removeComponent('collider'), e.hasComponent('mesh')
e.script('Patrol').radius // an object script's fields on this object
e.getVar('hp'), e.setVar('hp', 10) // instance variables
e.duplicate(), e.destroy()

editor.toast('Done')
await editor.prompt('Name?', 'Tree') // text, or null
await editor.promptNumber('How many?', 10) // a number, or null
await editor.confirm('Delete all trees?')
await editor.pick('Shape', ['box', 'sphere'])
THREE // three.js maths (vectors, quaternions…)

The code editor knows all of these: editor. lists them and mistakes are underlined.

Safe to open anyone's project​

Editor scripts run apart from the editor page (in a Web Worker). They can only use the editor API above. They can't read your other projects, your sign-in or your GitHub link, and they can't hang the editor. Opening a project from someone else is as safe with editor scripts as without.

console.log in an editor script goes to the browser's console. Use editor.toast to show a message in the editor.