Decorator

    The parameter name of the decorator ccclass specifies the name of the cc class, which is unique, even same ccclass name in scripts in different sub folder is forbidden and considered as conflicts. When the corresponding cc class needs to be retrieved, it can be found by its cc class name, e.g.:

    • Serialization. If the object is a cc class object, the cc class name of the object will be recorded during serialization, and the corresponding cc class will be found for serialization based on this name during deserialization.

    • When the cc class is a component class, Node can look up the component by the cc class name of the component class.

    These decorators are used to decorate sub classes of Component.

    By default, all components are executed only at runtime, meaning that their lifecycle callbacks are never triggered in editor mode. executeInEditMode allows the current component to be executed in editor mode, the default value of this decorator is false.

    1. const { ccclass, executeInEditMode } = _decorator;
    2. @ccclass('Example')
    3. @executeInEditMode(true)
    4. export class Example extends Component {
    5. update (dt: number) {
    6. // Will be executed in editor environment
    7. }
    8. }

    requireComponent

    The requireComponent decorator is used to specify a dependent component for the current component, the default value is null. When a component is added to a node, the engine will automatically add the dependent component to the same node if the dependent component does not exist yet, preventing script errors. This behavior is also valid at runtime.

    1. const { ccclass, requireComponent } = _decorator;
    2. @ccclass('Example')
    3. @requireComponent(Sprite)
    4. export class Example extends Component {
    5. }

    executionOrder

    executionOrder is used to specify the execution priority of component lifecycle callbacks. The execution ordering is described as follows.

    • For different components on the same node, those with smaller executionOrder are executed first, and those with the same executionOrder are executed in the order in which they are added.
    • For the same component on different nodes, the order of execution is determined by the node tree.

    This ordering setting is only valid for onLoad, onEnable, start, update and lateUpdate, but not for onDisable and onDestroy.

    1. const { ccclass, executionOrder } = _decorator;
    2. @ccclass('Example')
    3. @executionOrder(3)
    4. export class Example extends Component {
    5. }

    Only allow one component of the same type (with subclasses) to be added to the same node to prevent logic conflicts, the default value is false.

    1. const { ccclass, disallowMultiple } = _decorator;
    2. @ccclass('Example')
    3. @disallowMultiple(true)
    4. export class Example extends Component {
    5. }

    menu

    @menu(path) is used to add the current component to the component menu to make it easier for the user to find it.

    help

    Specify the URL of the current component’s help page. Once set, a help icon will appear in the Inspector panel and can be clicked to open the specified page.

    1. const { ccclass, help } = _decorator;
    2. @ccclass('Example')
    3. }

    The decorator property is applied to a property or accessor of a cc class. Similar to the ccclass decorator, the property decorator injects additional information to control Cocos Creator’s serialization of the property, the presentation of the property in the Inspector panel, and so on.

    The various features of the property decorator are specified via its attributes in @property({}). Usage of all attributes can be found in: .

    1. @property({
    2. type: Node,
    3. visible: true,
    4. })
    5. targetNode: Node | null = null;

    Next, some of the important property attributes are listed below.

    The type attribute specifies a type recognizable by the editor. The type can be specified with several forms of arguments.

    • Built-in primitive type:

      CCInteger, CCFloat, CCBoolean, and CCString are built-in property type identifiers that generally work on array properties. Non-array types usually do not need to explicitly declare a type.

      • CCInteger declares the type as integer.
      • CCFloat declares the type as floating point.
      • CCString declares the type as String.
      • CCBoolean declares the type as Boolean.
    • Other cc class type

      All properties with cc class type need to explicitly specify its type, otherwise the editor will not be able to recognize the type and how to serialization the property.

    • Array type

      When use the built-in primitive type or the cc class type as array element type for an array property, the property can be declared using the array type. For example, [CCInteger], [Node] will let the Inspector panel present the property as an array of integers and an array of nodes, respectively.

    If the property does not specify a type, Cocos Creator will derive its cc type from the property’s default value or the result of an initialization formula:

    • If the type of the property is the JavaScript primitive types number, string, and boolean, then the property type is CCFloat, CCString, and CCBoolean, respectively.
    • Under other circumstances, the property’s type will be undefined, and the editor will prompt a Type(Unknown) notice in the Inspector panel.

    The following code demonstrates the declaration of properties for different cc types.

    1. import { _decorator, CCInteger, Node, Enum } from 'cc';
    2. const { ccclass, property, integer, float, type } = _decorator;
    3. enum A {
    4. c,
    5. d
    6. }
    7. Enum(A);
    8. @ccclass
    9. class MyClass {
    10. @property // JavaScript primitive type, automatically recognized as Creator's floating-point type by default.
    11. index = 0;
    12. @property(Node) // Declare property cc to be of type Node, which is equivalent to @property({type: Node}) when the property parameter is only type.
    13. targetNode: Node | null = null; // Equivalent to targetNode: Node = null!
    14. // Declare the cc type of the property children to be a Node array
    15. @property({
    16. type: [Node]
    17. })
    18. children: Node[] = [];
    19. @property({
    20. type: String,
    21. }) // Warning: the constructor String should not be used. equivalent to CCString. or you can choose not to declare the type
    22. text = '';
    23. @property
    24. children2 = []; // Undeclared cc type, inferring from initialization result that the elements are undefined arrays
    25. @property
    26. @property({ type: A })
    27. }

    For convenience, several additional decorators are provided to quickly declare cc types. If you only need to declare type attribute of the property, you can use the following decorators instead of @property:

    1. import { _decorator, CCInteger, Node } from 'cc';
    2. const { ccclass, property, integer, float, type } = _decorator;
    3. @ccclass
    4. class MyClass {
    5. @integer // declare the cc type of the property as an integer
    6. index = 0;
    7. @type([Node]) // Declare the cc type of the property children to be a Node number
    8. children: Node[] = [];
    9. @type(String) // Warning: should not use constructor String. equivalent to CCString. can also choose not to declare type
    10. text = '';
    11. // JavaScript primitive types `number`, `string`, `boolean` can usually be undeclared
    12. // You can just write
    13. @property
    14. text = '';
    15. }

    visible attribute

    To force display in the Inspector panel, set the visible attribute to true:

    To force hiding, set the visible attribute to false:

    1. @property({ visible: false })
    2. num = 0;

    serializable attribute

    Properties are serialized by default. Once serialized, the property values set in the editor will be saved to the scene and other resource files, and will be automatically restored to the set property values when the scene is loaded. To not serialize, set serializable as false.

    1. @property({ serializable: false })
    2. num = 0;

    All properties are inherited by subclasses. If a subclass wants to override a property of the same name of the parent class, the override attribute needs to be set explicitly, otherwise, there will be a renaming warning:

    1. @property({ tooltip: "my id", override: true })
    2. id = "";

    group attribute

    If there are many properties or mixed properties defined in the script, the properties can be grouped and sorted by group for easy management. It also supports sorting properties within a group.

    • @property({ group: { name } })

    • @property({ group: { id, name, displayOrder, style } })

    PropertyDescription
    idGroup ID, string type, is a unique identifier for the property group, and defaults to default.
    nameThe name to classify the properties in the group, string type.
    displayOrderSort the groups, number type. The smaller the number, the higher the sorting. The default is Infinity, which means the group is sorted last.
    If there are multiple groups without displayOrder set, they will be sorted in the order declared in the script.
    styleGrouping styles, currently only tab styles are supported.

    Example script is as follows:

    1. import { _decorator, Component, Label, Sprite } from 'cc';
    2. const { ccclass, property } = _decorator;
    3. @ccclass('SayHello')
    4. export class SayHello extends Component {
    5. // Group 1
    6. // The property category named "bar" within the group, which contains a Label property named "label".
    7. @property({ group: { name: 'bar' }, type: Label })
    8. label: Label = null!;
    9. // The property category named "foo" within the group, which contains a Sprite property named "sprite".
    10. @property({ group: { name: 'foo' }, type: Sprite })
    11. sprite: Sprite = null!;
    12. // Group 2
    13. // The property category named "bar2" within the group, which contains a Label property named "label2" and a Sprite property named "sprite2".
    14. @property({ group: { name: 'bar2', id: '2', displayOrder: 1 }, type: Label })
    15. label2: Label = null!;
    16. @property({ group: { name: 'bar2', id: '2' }, type: Sprite })
    17. sprite2: Sprite = null!;
    18. }

    Mounting the script to the node displays the following image in the Inspector panel:

    decorator-group

    Because group 1 does not specify displayOrder and group 2 specifies displayOrder as 1, group 2 will be ranked ahead of group 1.

    Sorting the properties within a group can also be done via displayOrder. Taking group 2 as an example, it is currently sorted in the order defined in the script, with label2 in front of sprite2. Let’s adjust it to:

    Back to the editor, the sprite2 is now in front of label2 in the Inspector panel: