Listening events
Introduction
The editor provides some helpers for listening events in the scene. Those helpers are provided as decorators and can be used in any attached script in the scene.
Each decorator can be used to decorate a method in the class. Method that will be called each time an event of the provided type(s) is raised in the scene.
Those decorators are available in the babylonjs-editor-tools package that is provided as a depdendency in the package.json file. In case a decorator that is documented here is not available in the code, make sure to install the up-to-date package in your project.
@onPointerEvent
When a method is decorated with @onPointerEvent, this method will be called each time the provided pointer event type(s) is raised in the scene:
Multiple event types can be listened at the same time by providing an array of event types to the decorator.
The decorated method always receives a parameter of type PointerInfo that contains more information about the event that has been raised.
The decorated method always receives a parameter of type PointerInfo that contains more information about the event that has been raised.
Filtering per mesh
By default, the @onPointerEvent decorator listens for global events. In other words, anywhere the pointer event is raised in the scene, the decorated method will be called.
Scripts that are attached to meshes (extending AbstractMesh class) can listen for events that are raised only on the attached mesh by changing the listening mode.
The following mode is only available for scripts attached to meshes. If the attached object is not a Mesh then an error will be thrown at runtime when loading the scene.
Including descendants
When importing meshes, from a GLB files for example, it can be useful to listen for events on the entire hierarchy of imported meshes. Especially when the imported hierarchy is complex and contains multiple meshes.
To do so, the listening mode can be set to includeDescendants and the decorated method will be called when the event is raised on the attached mesh or any of its descendants.
That way, this mode is available on any node (TransformNode, Light, etc.) the script is attached to and is not limited to meshes only like the mode attachedMeshOnly.
@onKeyboardEvent
As well as @onPointerEvent decorator, the editor provides a @onKeyboardEvent decorator that can be used to listen for keyboard events in the scene.
When a method is decorated with @onKeyboardEvent, this method will be called each time the provided keyboard event type(s) is raised in the scene:
As well as for pointer events, multiple event types can be listened at the same time by providing an array of event types to the decorator:
Next
Compressing textures