SceneObject
Type | Name | Interface Description |
---|---|---|
Variables | enabled: boolean | • Function: Indicates whether the object is enabled. If the object is disabled, it may not be rendered or updated in the scene. |
Variables | layer: number | • Function: Defines the level where the object is located, used for hierarchical rendering or hierarchical logical processing. |
Variables | scene: Scene [Read Only] | • Function: A read-only property indicating the scene to which this object belongs. |
Variables | parent: SceneObject | null | • Function: Stores the parent object of this object, or undefined if there is no parent object. Used to build the object hierarchy. |
Examples
enabled: boolean
sceneObject.enabled = false
layer: number
const layer = sceneObject.layer;
scene: Scene [Read Only]
const scene = sceneObject.scene;
parent: SceneObject | null
object1.parent = object2;
Use Case
@component()
export class NewBehaviourScript extends APJS.BasicScriptComponent {
onStart() {
const obj = this.getSceneObject();
console.log("enabled:", obj.enabled);
console.log("layer:", obj.layer);
console.log("parent:", obj.parent?.name);
}
onUpdate(deltaTime: number) {
}
}
Type | Name | Interface Description |
---|---|---|
Functions | addComponent(type: string): Component | null | • Function: Adds a component of the specified type to this object. • Return Value: Returns the added component object; if it fails, returns undefined. • Note: The current version (Phase I) only supports adding components of the camera type, and other types of components may be added in the future. |
Functions | getComponent(type: string): Component | null | • Function: Get the component of the specified type on this object. • Return value: Returns the found component object; if not found, returns undefined. |
Functions | getComponents(type?: string): Component[] Unopened components are not in the list | • Function: Get all components of the specified type on this object. If no type is specified, return all components. • Return Value: Returns an array of component objects. |
Functions | getComponentsRecursive(type: string): Component[] | • Function: Recursively obtain all components of the specified type on this object and all its child objects. • Return Value: Returns an array of component objects. |
Functions | getTransform(): Transform | • Function: Get the Transform component of the object, which is used to handle transformation operations such as position, rotation, and scaling. • Return Value: Returns the Transform component of this object. |
Functions | removeComponent(component: Component): boolean | • Function: Remove the specified component from this object. • Return value: If the removal is successful, returns true; otherwise, returns false. |
Functions | getChildren(): SceneObject[] | • Function: Get all child objects of this object. • Return value: Returns an array of child objects. |
Examples
getChild(name: string): SceneObject | null
let child = myObject.getChild("ChildObjectName");
addComponent(type: string): Component | null
let cameraComponent = myObject.addComponent("camera");
getComponent(type: string): Component | null
let cameraComponent = myObject.getComponent("Camera");
getComponents(type?: string): Component[]
Unopened components are not in the list
let allCameraComponents = myObject.getComponents("camera");
getComponentsRecursive(type: string): Component[]
let allCameraComponentsRecursive = myObject.getComponentsRecursive("camera");
getTransform(): Transform
let transform = myObject.getTransform();
removeComponent(component: Component): boolean
let success = myObject.removeComponent(cameraComponent);
getChildren(): SceneObject[]
let children = myObject.getChildren();
Scenario Examples
Main Camera
// Create a new SceneObject and name it "MainCamera"
let mainCamera = this.scene.createSceneObject("MainCamera");
// Add a Camera component to the MainCamera object
let cameraComponent = mainCamera.addComponent("camera");
// Check whether the Camera component was successfully added
if (cameraComponent) {
console.log("Camera component added to MainCamera.");
}
// Enable the MainCamera object
mainCamera.enabled = true;
Adding some objects to the scene
// Create a new SceneObject and name it "Object1"
let object1 = this.scene.createSceneObject("Object1");
// Create a new SceneObject and name it "Object2"
let object2 = this.scene.createSceneObject("Object2");
// Enable these objects (Object1 and Object2)
object1.enabled = true;
object2.enabled = true;
Finding and Manipulating Objects
// Find the object named "Object1"
let foundObject = this.scene.findSceneObject("Object1", null);
if (foundObject) {
console.log(`Found object: ${foundObject.name}`);
}
// Get all child objects of Object1
let children = object1.getChildren();
console.log(`Object1 has ${children.length} children.`);
// Get the parent object of Object2
let parent = object2.parent;
if (parent) {
console.log(`Object2's parent is: ${parent.name}`);
}