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:
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.
import { Behaviour, serializable } from "@needle-tools/engine";
export class Rotate extends Behaviour
{
@serializable()
speed : number = 1;
start(){
// Logging is useful for debugging in the browser
// Open the developer console (F12) to see your component's data
console.log(this);
}
// update will be called every frame
update(){
this.gameObject.rotateY(this.context.time.deltaTime * this.speed);
}
}
import { Behaviour } from "@needle-tools/engine";
export class PrintNumberComponent extends Behaviour
{
start(){
this.printNumber(42);
}
private printNumber(myNumber : number){
console.log("My Number is: " + myNumber);
}
}
export class MyComponent1 extends Behaviour { }
export class MyComponent2 extends Behaviour { }
import { Behaviour, serializable } from "@needle-tools/engine";
export class MyComponent extends Behaviour
{
@serializable()
speed: number = 5;
@serializable()
playerName: string = "Player";
@serializable()
isActive: boolean = true;
}
import { Behaviour, serializable } from "@needle-tools/engine";
import { Object3D, Light, Camera } from "three";
export class MyComponent extends Behaviour
{
@serializable(Object3D)
myTarget?: Object3D;
@serializable(Light)
targetLight?: Light;
@serializable(Camera)
mainCamera?: Camera;
}