Common Node and Component Interfaces
Please review the Node and the API documentation.
Suppose you are inside a running component script, to access a node inside the current script use .
A node is activated by default. It’s activation state can be changed in code by setting the node’s active
property. Example:
Setting the 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 active
to true
will immediately trigger the following activation operations:
- 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
onEnable
methods on these components, these methods will be executedthis.node.active = false;
- 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
update
in these components will no longer be executed - If there are methods 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.node
.
This is valid:
This is also valid:
// method 2
this.node.removeFromParent();
parentNode.addChild(this.node);
These two methods are equivalent.
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.
Changing Node Location
There are two ways:
Use the
setPosition
method:this.node.setPosition(100, 50, 100);
// Or
this.node.setPosition(new Vec3(100,50,100));
Setting the variable:
The above two usages are equivalent.
Example:
this.node.setRotation(quaternion);
Or set local rotation by Euler angle:
this.node.setRotationFromEuler(90,90,90);
Changing Node Scale
Example:
this.node
: The node instance to which this component belongs.this.enabled
: Whether to execute theupdate
method 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’senabled
property istrue
, the code in it will be executed every frame.start()
: It will be executed before the firstupdate
of the component, usually used for logic that needs to be executed after theonLoad
of all components is initialized.