In Extensions, custom public properties automatically generate corresponding settings items, while private properties do not. Private properties are those that start with #, whereas public properties include both directly defined public properties and those defined through getter methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classExtensionSample { // Private property, will not generate a setting item #a = 1; // Public property, will generate a setting item abc = "123"; // Public getter, will generate a setting item geta() { returnthis.#a; } // Public setter, will generate a setting item seta(a) { this.#a = a; } } exportdefaultExtensionSample;
The above code will generate the following settings:
2. Custom Property Types
In the example above, the generated settings item for abc is of type string, while a is of type number. Below are all the types described.
2.1 Number
1 2 3 4 5
classPropertySample { // Assigning a default value of a number means the property will be considered a number type setting = 123; } exportdefaultPropertySample;
The style of number properties is generated as follows:
2.2 Number Slider
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classPropertySample { // Public property will generate a setting item setting = Decimal.default(10, { min: -180, max: 180, type: "int", step: 1, // The step value for the slider unit: "degrees", // Unit to display after the input box showInput: true, // Whether to show the input box inputWidth: 50, // Width of the input box }); onPropertyChanged(property) { if (property === "setting") { // Get the int value console.log(this.setting.value); } } } exportdefaultPropertySample;
Number slider property is generated with the following effect:
2.3 Strings
1 2 3 4 5
classPropertySample { // Assigning a default value as a string means this property will be considered a string property setting = "Hello World"; } exportdefaultPropertySample;
The style for generating string properties is as follows:
2.4 Widget
1 2 3 4 5 6 7 8 9 10 11
classPropertySample { // Component properties initialized with Element.empty() do not support setting a specific component as the default value. setting = Element.empty(); onPropertyChanged(property) { if (property == "setting") { const element = this.setting; console.log("element name is " + element.name); } } } exportdefaultPropertySample;
The style for generating wdget properties is as follows:
2.5 Widget array
1 2 3 4 5 6 7 8 9 10 11 12 13
classPropertySample { // Component properties initialized with ElementArray.empty() do not support setting specific components as default values. setting = ElementArray.empty(); onPropertyChanged(property) { if (property == "setting") { for (let i = 0; i < this.setting.length; i++) { const element = this.setting[i]; console.log("element " + i + " name is " + element.name); } } } } exportdefaultPropertySample;
The effect of widget array properties generation is as follows:
classPropertySample { // Public property will generate settings. setting = Vector.default([0, 0], { generics: [ { key: "x", type: "int" }, { key: "y", type: "int" }, ], // Unit displayed after the input box unit: "meters", // Whether to hide key values in front of the input box // Can be set to true when space is limited hideGeneric: false, }); onPropertyChanged(property) { if (property == "setting") { // Value is in array form [0,0] console.log(this.setting.value); } } } exportdefaultPropertySample;
The effect of vector properties generation is as follows:
2.7 Data Field
1 2 3 4 5 6 7 8 9 10 11 12 13
classPropertySample { // Public property will generate settings, which will appear in "Data - Secondary Development Data Fields" setting = Field.default({ minFields: 1, // Minimum number of fields required maxFields: 3, // Maximum number of fields allowed }); asyncinit() { // Read the data set in this data field const data = awaitthis.setting.readDataAsync(); console.log("data", data); } } exportdefaultPropertySample;
classPropertySample { // Public property that will generate a setting item // The default value can be a URL // It can also be a string array. If the default is a string array, isMultiple will be forced to true setting = File.default( "https://www.shanhaibi.com/wp-content/themes/twentyseventeen/assets/images/header/home_icon_black.png", { format: "image|video|.pdf|model|.mp3", isMultiple: false, } ); onPropertyChanged(property) { if (property == "setting") { // The value of the file property is the file URL // If isMultiple:true is set // It will be an array of URLs console.log(this.setting.value); } } } exportdefaultPropertySample;