Automatic Component Generation
Automatically generating Editor components
When working in Unity or Blender then you will notice that when you create a new Needle Engine component in Typescript or Javascript it will automatically generate a Unity C# stub component OR a Blender panel for you.
This is thanks to the Needle component compiler that runs behind the scenes in an editor environment and watches changes to your script files. When it notices that you created a new Needle Engine component, it will generate a Unity C# stub component or a Blender Python component schema for you. These generated components include public variables, properties and methods, that you can then set from within the Editor. You can also wire up events to methods in your components.
Note: The component compiler currently only generates components. You can reference custom classes and types (like ScriptableObjects or enums), but you need to make sure that the class exists in both C# and TypeScript with a matching name.
Standalone CLI Usage
You can also run the component compiler directly from the command line without any install using npx:
npx @needle-tools/needle-component-compiler <target> <output_dir> <input_files...>target
csharp or blender
output_dir
Directory for generated files
input_files
One or more .ts source files (globs supported)
Examples
# Generate C# component stubs
npx @needle-tools/needle-component-compiler csharp ./codegen ./src/MyComponent.ts
# Generate Blender component schemas
npx @needle-tools/needle-component-compiler blender ./codegen ./src/MyComponent.ts
# Process multiple files
npx @needle-tools/needle-component-compiler csharp ./codegen ./src/**/*.tsYou can also use the compiler as a programmatic API:
Component Compiler in Unity
If you want to add scripts inside the src/scripts folder in your project then you need to have a Component Generator on the GameObject with your Needle Engine component. Now when adding new components in your/threejs/project/src/scriptsit will automatically generate Unity scripts in Assets/Needle/Components.codegen. If you want to add scripts to any NpmDef file you can just create them - each NpmDef automatically watches script changes and handles component generation, so you don't need any additional component in your scene.
For C# fields to be correctly generated, it is important that you declare a Typescript type. The types can either be basic types like number, string, boolean, or custom classes. Custom classes need to be available in the Unity project as well.
Here is an example. The TypeScript file is what has been placed in src/scripts/MyCustomComponent.ts. The component compiler automatically generates the C# stub component.
::: code-tabs @tab Typescript
@tab Generated CSharp
@tab Extending the generated component
:::
Extending generated components
The generated C# classes for components are generated with the partial flag so that it is easy to extend them with functionality. This is helpful to draw gizmos, add context menus or add additional fields or methods that are not part of a built-in component.
See the code tabs above for an example of how to extend the generated component. You can add custom methods, properties, or even custom editors for your component. Just make sure to add code outside the // NEEDLE_CODEGEN_ blocks so that it does not get overwritten by the component compiler.
:::tip Member Casing Exported members will start with a lowercase letter. For example if your C# member is named MyString it will be assigned to myString. :::
Controlling component generation
You can use // comments above classes, fields, or methods to control code generation:
// @generate-component
Class
Force generation of next class (not required, classes are generated by default)
// @dont-generate-component
Class
Skip generating this class, useful when you already have an existing C# script
// @type MyNamespace.MyType
Class / Field
Override the resolved C# type or base class
// @abstract
Class
Mark the generated class as abstract
// @serializeField
Field
Emit [SerializeField] attribute on the generated field
// @nonSerialized
Field / Method
Skip generating C# code for this member
// @tooltip "My text"
Field
Emit [Tooltip("My text")] (C#) or "description" (Blender). This is optional — JSDoc comments are automatically used as tooltips
// @contextmenu "Label"
Method
Emit [ContextMenu("Label")] on the generated method
// @ifdef MY_DEFINE
Field
Wrap the generated field in a #if MY_DEFINE preprocessor block
Examples
Force the component compiler to generate a C# AudioClip field named myAudioClip
Force the component compiler to derive from a specific subclass
JSDoc Tooltips
JSDoc comments (/** ... */) on fields are automatically converted to tooltips. The text is sanitized (inline code, URLs, images, and JSDoc tags are stripped) to produce clean tooltip text. An explicit // @tooltip directive takes priority over a JSDoc comment on the same field.
::: code-tabs @tab Typescript
@tab Generated C#
@tab Generated Blender Schema
:::
Last updated