Common Node and Component Interfaces
After obtaining a Node or Component instance through the method introduced in the Access Node and Component documentation, there are common interfaces that can be used to achieve the various effects needed through the node and component instance and it's operation.
Node Status and Level Operations
Suppose you are inside a running component script, to access a node inside the current script use
Activating and Deactivating a Node
A node is activated by default. It's activation state can be changed in code by setting the node's
active property. Example:
this.node.active = false;
active property and switching the active and closed states of the node in the editor have the same effect. When a node is down, all its components will be disabled. At the same time, all its child nodes and components on the child nodes will also be disabled. It should be noted that when child nodes are disabled, their
active properties are not changed, so they will return to their original state when the parent node is reactivated.
In other words,
active actually represents the active state of the node itself, and whether this node current can be activated depends on its parent node. And if it is not in the current scene, it cannot be activated. We can use the read-only property
activeInHierarchy on the node to determine whether it is currently activated. Example:
this.node.active = true;
If the node is in the can be activated state, modifying
true will immediately trigger the following activation operations:
- Reactivate the node and all child nodes under the node whose active is true in the scene
- All components on this node and all child nodes will be enabled, and the update method in them will be executed every frame afterwards
If there are
onEnablemethods on these components, these methods will be executed
this.node.active = false;
If the node is already activated, changing
false will immediately trigger the following shutdown operations:
- Hide the node and all child nodes under the node in the scene
- All components on this node and all child nodes will be disabled, that is, the code in
updatein these components will no longer be executed
- If there are
onDisablemethods on these components, these methods will be executed
Change the Parent Node of a Node
Suppose the parent node is
parentNode and the child node is
This is valid:
// method 1 this.node.parent = parentNode;
This is also valid:
// method 2 this.node.removeFromParent(); parentNode.addChild(this.node);
These two methods are equivalent.
removeFromParentusually needs to pass a
false, otherwise it will empty the bound events and actions, etc. on the node by default in versions prior to v3.0.
- After creating a new node through the method introduced in the Create and Destroy Node documentation, it is a must to set a parent node for the node to correctly initialize the node.
Child Nodes of the Parent Node
this.node.children will return an array of all child nodes of the node.
this.node.children.length will return the number of children of the node.
Note: the above two APIs will only return the direct child nodes of the node, not the child nodes of the child node.
Changing Node Transformations (position, rotation, scaling)
Changing Node Location
There are two ways:
this.node.setPosition(100, 50, 100); // Or this.node.setPosition(new Vec3(100,50,100));
this.node.position = new Vec3(100,50,100);
The above two usages are equivalent.
Changing Node Rotation
Or set local rotation by Euler angle:
Changing Node Scale
Common Component Interface
Component is the base class of all components, and any component includes the following common interfaces (assuming that we use this to refer to this component in the script of the component):
this.node: The node instance to which this component belongs.
this.enabled: Whether to execute the
updatemethod of the component every frame, and also to control whether the renderable component is displayed.
update(deltaTime: number): As a member method of the component, when the component's
true, the code in it will be executed every frame.
onLoad(): Executed when the node where the component is located is initialized (when the node is added to the node tree).
start(): It will be executed before the first
updateof the component, usually used for logic that needs to be executed after the
onLoadof all components is initialized.