githubEdit

Create Components

Learn how to write custom TypeScript and JavaScript components for Needle Engine. Create interactive behaviors, use the @serializable decorator for editor integration, and leverage lifecycle methods l

Learn how to create interactive components for Needle Engine using TypeScript or JavaScript.

:::tip Prerequisites New to TypeScript? Start here:

:::details Video: Creating Custom Components in Unity This video gives a short introduction to the easiest and fastest way to create custom Needle Engine components in Unity.

:::


Quick Start

Direct File Approach

Add a .ts or .js file to src/scripts/ in your web project:

your-project/
└── src/
    └── scripts/
        └── MyFirstScript.ts  ← Add your component here

Benefits:

  • Simple and direct

  • Perfect for small projects

  • Automatic hot reload

Unity - NPM Definition Approach

Organize code into reusable npm packages using NPM Definition files:

  1. In Unity: Create > NPM Definition

  2. Right-click the NpmDef: Create > TypeScript

  3. Write your component

Benefits:

  • Modular code organization

  • Share code between projects

  • Standard npm package format

Learn more about NPM Definitionsarrow-up-right


Your First Component

Create src/scripts/Rotate.ts:

What happens next:

  • 🔄 C# stub (Unity) or Blender panel auto-generates on save

  • Hot reload in browser—no Unity recompilation needed

  • 🚀 Instant iteration—see changes in ~1 second


Component with Custom Function


Multiple Components Per File

You can export multiple components from one file:


Component Architecture

Components attach to three.js Object3D instances:

  • Access the Object3D: this.gameObject

  • Access the scene: this.context.scene

  • Access components: this.gameObject.getComponent(Type)

:::warning Visibility & Active State Setting visible = false on an Object3D acts like Unity's SetActive(false):

  • Disables all components on this object and its children

  • No update events called until visible = true again

  • To hide visually without affecting components, disable the Renderer component instead :::


Serialization

Use @serializable() to expose properties in the editor (Unity/Blender) and ensure they get saved/loaded correctly.

Basic Types

Primitives (number, string, boolean) only need @serializable():

Object References

For object references and complex types, you must specify the type:

Arrays

Primitive arrays don't need a type:

Arrays with object references need the type specified:

:::tip Reference See the complete @serializable decorator referencearrow-up-right for more details and advanced usage. :::


Version Control

While generated C# components use the type name to produce stable GUIDs, we recommend checking in generated components in version control as a good practice.


Next Steps


Need Help?

Last updated