Script parameters are a very useful feature in the script system. With this feature, you can expose parameters in the script to the editor, allowing them to be configured in the scene editor. You can directly modify various properties of the script in the interface without delving into the code. This intuitive editing method allows non-professional developers to easily debug various states in the script.
import { Script } from '@galacean/engine';
import { inspect } from "@galacean/editor-decorators";
export default class extends Script {
@inspect('Number')
rotate = 1;
onUpdate(deltaTime: number) {
this.entity.transform.rotate(this.rotate, this.rotate, this.rotate);
}
}
In the above code, we declare a rotate
property of type Number
using the @inspect
decorator and expose it to the editor.
Currently supported parameter types are:
Number
: Number typeInput
: Input boxSlider
: SliderBoolean
: Boolean typeVector2
: 2D vectorVector3
: 3D vectorVector4
: 4D vectorRect
: RectangleColor
: Color picker, supports RGBAAssetPicker
: Asset pickerSelect
: Dropdown selectorTextarea
: Multi-line text input boxThe second parameter of the @inspect
decorator is an object used to configure various properties of the corresponding type parameter. Different parameter types have different options. For example, Number
and Slider
have min
and max
configurations, and Select
has options
configuration. For more configurable properties, you can check @galaean/editor-decorators. Below, taking the number selector as an example, we introduce the meaning of each configuration.
import { Script } from '@galacean/engine';
import { inspect } from "@galacean/editor-decorators";
export default class extends Script {
@inspect('Number', {
min: 0, // 最小值
max: 10, // 最大值
dragStep: 0.1, // 拖拽步长
property: 'rotate', // 对应到引擎对象的属性名,默认为装饰器所修饰的属性名
label: 'Rotate', // 在检查器面板中显示的名称,默认为装饰器所修饰的属性名
info: 'Rotate speed', // 在检查器面板中显示的描述信息
})
rotate = 1;
onUpdate(deltaTime: number) {
this.entity.transform.rotate(this.rotate, this.rotate, this.rotate);
}
}