Automatic Component Generation
Last updated
Last updated
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 magic of the 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 then generate the correct Unity component or Blender panel including public variables or properties that you can then set or link from within the Editor.
You can use the following comments in your typescript code to control C# code generation behavior:
// @generate-component
Force generation of next class
// @dont-generate-component
Disable generation of next class, this is useful in cases where you already have an existing C# script in your project
// @serializeField
Decorate generated field with [SerializeField]
// @type UnityEngine.Camera
Specify generated C# field type
// @nonSerialized
Skip generating the next field or method
Examples
Force the component compiler to generate a C# AudioClip field named myAudioClip
Force the component compiler to derive from a specific subclass
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 ExportInfo
component.
Now when adding new components in your/threejs/project/src/scripts
it 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 currently important that you explictly declare a Typescript type. For example myField : number = 5
You can switch between Typescript input and generated C# stub components using the tabs below ::: code-tabs @tab Typescript
@tab Generated C#
@tab Extending Generated C#
:::
:::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
.
:::
Component C# classes are generated with the 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.