The SceneSwitcher can be used to dynamically load and unload extra content
Available scenes are defined in the scenes array.
Loaded scenes will be added to the SceneSwitcher's GameObject as a child and removed when another scene is loaded by the same SceneSwitcher.
Live Examples

Use the ISceneEventListener interface to listen to scene open and closing events with the ability to modify transitions and stall the scene loading process.

  • loadscene-start: Called when a scene starts loading
  • loadscene-finished: Called when a scene finished loading
  • progress: Called when a scene is loading and the progress changes
  • scene-opened: Called when a scene is loaded and added to the SceneSwitcher's GameObject
sceneSwitcher.addEventListener("loadscene-start", (e) => {
console.log("Loading scene", e.detail.scene.url);
});
sceneSwitcher.addEventListener("loadscene-finished", (e) => {
console.log("Finished loading scene", e.detail.scene.url);
});
sceneSwitcher.addEventListener("progress", (e) => {
console.log("Loading progress", e.loaded, e.total);
});
sceneSwitcher.addEventListener("scene-opened", (e) => {
console.log("Scene opened", e.detail.scene.url);
});

Hierarchy (View Summary)

Properties

autoLoadFirstScene: boolean = true

When enabled the first scene will be loaded when the SceneSwitcher becomes active

true
clamp: boolean = true

When enabled the current scene index will be clamped to the scenes array bounds.
For example when the last scene is loaded and clamp is true then trying to load the next() scene will not change the scene.
When clamp is false and the last scene is loaded then the first scene will be loaded instead.

true
createMenuButtons: boolean = false

When enabled will create a button for the Needle menu to switch to the next or previous scene

false
gameObject: GameObject

Reference to the GameObject this component is attached to This is a three.js Object3D with additional GameObject functionality

guid: string = "invalid"

Unique identifier for this component instance, used for finding and tracking components

loadingScene?: AssetReference

The scene that is displayed while a scene is loading.

undefined
preloadConcurrent: number = 2

how many scenes can be loaded in parallel

2
preloadNext: number = 1

how many scenes after the currently active scene should be preloaded

1
preloadPrevious: number = 1

how many scenes before the currently active scene should be preloaded

1
queryParameterName: string = "scene"

the url parameter that is set/used to store the currently loaded scene in, set to "" to disable

"scene"
sceneLoaded: EventList<SceneSwitcher> = ...

The sceneLoaded event is called when a scene/glTF is loaded and added to the scene

sceneLoadingStart: EventList<LoadSceneEvent> = ...

Called when a scene starts loading

scenes: AssetReference[] = []

The scenes that can be loaded by the SceneSwitcher.

[]
sourceId?: string

Identifier for the source asset that created this component. For example, URL to the glTF file this component was loaded from

useHistory: boolean = true

when enabled the new scene is pushed to the browser navigation history, only works with a valid query parameter set

true
useKeyboard: boolean = true

when enabled you can switch between scenes using keyboard left, right, A and D or number keys

true
useSceneBackground: boolean = true

When enabled will automatically apply the skybox from the loaded scene

true
useSceneLighting: boolean = true

when enabled will automatically apply the environment scene lights

true
useSceneName: boolean = true

when enabled the scene name will be used as the query parameter (otherwise the scene index will be used) Needs queryParameterName set

true
useSwipe: boolean = true

when enabled you can switch between scenes using swipe (mobile only)

true

Accessors

  • get activeAndEnabled(): boolean

    Checks if this component is currently active (enabled and part of an active GameObject hierarchy) Components that are inactive won't receive lifecycle method calls

    Returns boolean

    True if the component is enabled and all parent GameObjects are active

  • get context(): Context

    The context this component belongs to, providing access to the runtime environment including physics, timing utilities, camera, and scene

    Returns Context

  • set context(context: Context): void

    Parameters

    Returns void

  • get currentIndex(): number

    The index of the currently loaded and active scene

    Returns number

  • get currentLoadingProgress(): undefined | ProgressEvent<EventTarget>

    Get the progress of the currently loading scene. This is undefined if no scene is loading
    You can also subscribe to the loading event by adding an event listener to the scene switcher.
    For example like this sceneSwitcher.addEventListeneer("progress", (e) => {...})

    Returns undefined | ProgressEvent<EventTarget>

  • get currentlyLoadedScene(): undefined | AssetReference

    The currently loaded scene. This is undefined if no scene is loaded.

    Returns undefined | AssetReference

  • get currentlyLoadingScene(): undefined | AssetReference

    The currently loading scene. This is undefined if no scene is loading.

    Returns undefined | AssetReference

  • get destroyed(): boolean

    Checks if this component has been destroyed

    Returns boolean

    True if the component or its GameObject has been destroyed

  • get enabled(): boolean

    Controls whether this component is enabled Disabled components don't receive lifecycle callbacks

    Returns boolean

  • set enabled(val: boolean): void

    Parameters

    • val: boolean

    Returns void

  • get forward(): Vector3

    Gets the forward direction vector (0,0,-1) of this component's GameObject in world space

    Returns Vector3

  • get layer(): number

    The layer value of the GameObject this component is attached to Used for visibility and physics filtering

    Returns number

  • get name(): string

    The name of the GameObject this component is attached to Used for debugging and finding objects

    Returns string

  • set name(str: string): void

    Parameters

    • str: string

    Returns void

  • get right(): Vector3

    Gets the right direction vector (1,0,0) of this component's GameObject in world space

    Returns Vector3

  • get scene(): Scene

    Shorthand accessor for the current scene from the context

    Returns Scene

    The scene this component belongs to

  • get static(): boolean

    Indicates whether the GameObject is marked as static Static objects typically don't move and can be optimized by the engine

    Returns boolean

  • set static(value: boolean): void

    Parameters

    • value: boolean

    Returns void

  • get tag(): string

    The tag of the GameObject this component is attached to Used for categorizing objects and efficient lookup

    Returns string

  • set tag(str: string): void

    Parameters

    • str: string

    Returns void

  • get up(): Vector3

    Gets the up direction vector (0,1,0) of this component's GameObject in world space

    Returns Vector3

  • get worldEuler(): Euler

    Gets the rotation of this component's GameObject in world space as Euler angles (in radians)

    Returns Euler

  • set worldEuler(val: Euler): void

    Sets the rotation of this component's GameObject in world space using Euler angles (in radians)

    Parameters

    • val: Euler

      The world rotation Euler angles to set

    Returns void

  • get worldPosition(): Vector3

    Gets the position of this component's GameObject in world space.
    Note: This is equivalent to calling this.gameObject.worldPosition

    Returns Vector3

  • set worldPosition(val: Vector3): void

    Sets the position of this component's GameObject in world space

    Parameters

    • val: Vector3

      The world position vector to set

    Returns void

  • get worldQuaternion(): Quaternion

    Gets the rotation of this component's GameObject in world space as a quaternion Note: This is equivalent to calling this.gameObject.worldQuaternion

    Returns Quaternion

  • set worldQuaternion(val: Quaternion): void

    Sets the rotation of this component's GameObject in world space using a quaternion

    Parameters

    • val: Quaternion

      The world rotation quaternion to set

    Returns void

  • get worldRotation(): Vector3

    Gets the rotation of this component's GameObject in world space as Euler angles (in degrees)
    Note: This is equivalent to calling this.gameObject.worldRotation

    Returns Vector3

  • set worldRotation(val: Vector3): void

    Sets the rotation of this component's GameObject in world space using Euler angles (in degrees)

    Parameters

    • val: Vector3

      The world rotation vector to set (in degrees)

    Returns void

Methods

  • Registers an event listener for the specified event type

    Type Parameters

    Parameters

    • type: string

      The event type to listen for

    • listener: (evt: T) => any

      The callback function to execute when the event occurs

    Returns void

  • Add a scene to the SceneSwitcher.
    If the scene is already added it will be added again.

    Parameters

    • urlOrAssetReference: string | AssetReference

      The url of the scene or an AssetReference to the scene

    Returns AssetReference

    The AssetReference of the scene that was added

    // adding a scene:
    sceneSwitcher.addScene("scene1.glb");
    // add another scene and load it:
    const scene2 = sceneSwitcher.addScene("scene2.glb");
    sceneSwitcher.switchScene(scene2).then(res => { console.log("Scene loaded", res); });
  • Destroys this component and removes it from its GameObject After destruction, the component will no longer receive lifecycle callbacks

    Returns void

  • Dispatches an event to all registered listeners

    Parameters

    • evt: Event

      The event object to dispatch

    Returns boolean

    Always returns false (standard implementation of EventTarget)

  • Called at the beginning of each frame before regular updates. Use for logic that needs to run before standard update callbacks.

    Returns void

  • Called after all update functions have been called. Use for calculations that depend on other components being updated first.

    Returns void

  • Called after the scene has been rendered. Use for post-processing or UI updates that should happen after rendering

    Returns void

  • Called immediately before the scene is rendered.

    Parameters

    • frame: null | XRFrame

      Current XRFrame if in an XR session, null otherwise

    Returns void

  • Called before an XR session is requested Use to modify session initialization parameters

    Parameters

    • mode: XRSessionMode

      The XR session mode being requested

    • args: XRSessionInit

      The session initialization parameters that can be modified

    Returns void

  • Called when this component's collider begins colliding with another collider.

    Parameters

    • col: Collision

      Information about the collision that occurred

    Returns any

  • Called when this component's collider stops colliding with another collider.

    Parameters

    • col: Collision

      Information about the collision that ended

    Returns any

  • Called each frame while this component's collider is colliding with another collider

    Parameters

    • col: Collision

      Information about the ongoing collision

    Returns any

  • Called when the component is destroyed. Use for cleanup operations like removing event listeners

    Returns void

  • Called when the context's pause state changes.

    Parameters

    • isPaused: boolean

      Whether the context is currently paused

    • wasPaused: boolean

      The previous pause state

    Returns void

  • Called when this component's trigger collider is entered by another collider

    Parameters

    • col: ICollider

      The collider that entered this trigger

    Returns any

  • Called when another collider exits this component's trigger collider

    Parameters

    • col: ICollider

      The collider that exited this trigger

    Returns any

  • Called each frame while another collider is inside this component's trigger collider

    Parameters

    • col: ICollider

      The collider that is inside this trigger

    Returns any

  • Called when a field decorated with @validate() is modified.

    Parameters

    • Optionalprop: string

      The name of the field that was changed

    Returns void

  • Parameters

    • index: number

    Returns Promise<null | ArrayBufferLike> | Promise<boolean>

  • Reload the last scene that was loaded

    Returns Promise<boolean>

    a promise that resolves to true if the scene was loaded successfully

  • Removes a previously registered event listener

    Type Parameters

    Parameters

    • type: string

      The event type the listener was registered for

    • listener: (arg: T) => any

      The callback function to remove

    Returns void

  • Called when this component needs to remap guids after an instantiate operation.

    Parameters

    • guidsMap: GuidsMap

      Mapping from old guids to newly generated guids

    Returns void

  • Load a scene by its index in the scenes array.

    Parameters

    • index: string | number

      The index of the scene or a string that represents the scene uri (if the url is not known to the SceneSwitcher it will try to load the scene by its uri but it won't be added to the current scenes array. Use addScene to add a scene to the SceneSwitcher)

    Returns Promise<boolean>

    a promise that resolves to true if the scene was loaded successfully

  • Load the next scene in the scenes array (this.currentIndex + 1)
    If the current scene is the last scene in the array and this.clamp is disabled then the first scene will be loaded.

    Returns Promise<boolean>

    a promise that resolves to true if the scene was loaded successfully

  • Load the previous scene in the scenes array (this.currentIndex - 1)
    If the current scene is the first scene in the array and this.clamp is disabled then the last scene will be loaded.

    Returns Promise<boolean>

    a promise that resolves to true if the scene was loaded successfully

  • Sets the position of this component's GameObject in world space using individual coordinates

    Parameters

    • x: number

      X-coordinate in world space

    • y: number

      Y-coordinate in world space

    • z: number

      Z-coordinate in world space

    Returns void

  • Sets the rotation of this component's GameObject in world space using quaternion components

    Parameters

    • x: number

      X component of the quaternion

    • y: number

      Y component of the quaternion

    • z: number

      Z component of the quaternion

    • w: number

      W component of the quaternion

    Returns void

  • Sets the rotation of this component's GameObject in world space using individual Euler angles

    Parameters

    • x: number

      X-axis rotation

    • y: number

      Y-axis rotation

    • z: number

      Z-axis rotation

    • degrees: boolean = true

      Whether the values are in degrees (true) or radians (false)

    Returns void

  • Called once at the beginning of the first frame after the component is enabled. Use for initialization that requires other components to be awake.

    Returns void

  • Starts a coroutine that can yield to wait for events. Coroutines allow for time-based sequencing of operations without blocking. Coroutines are based on generator functions, a JavaScript language feature.

    Parameters

    • routine: Generator

      Generator function to start

    • evt: FrameEvent = FrameEvent.Update

      Event to register the coroutine for (default: FrameEvent.Update)

    Returns Generator

    The generator function that can be used to stop the coroutine

    Time-based sequencing of operations

    *myCoroutine() {
    yield WaitForSeconds(1); // wait for 1 second
    yield WaitForFrames(10); // wait for 10 frames
    yield new Promise(resolve => setTimeout(resolve, 1000)); // wait for a promise to resolve
    }

    Coroutine that logs a message every 5 frames

    onEnable() {
    this.startCoroutine(this.myCoroutine());
    }
    private *myCoroutine() {
    while(this.activeAndEnabled) {
    console.log("Hello World", this.context.time.frame);
    // wait for 5 frames
    for(let i = 0; i < 5; i++) yield;
    }
    }
  • Stops a coroutine that was previously started with startCoroutine

    Parameters

    • routine: Generator

      The routine to be stopped

    • evt: FrameEvent = FrameEvent.Update

      The frame event the routine was registered with

    Returns void

  • Determines if this component supports a specific XR mode

    Parameters

    • mode: XRSessionMode

      The XR session mode to check support for

    Returns boolean

    True if the component supports the specified mode

  • Switch to a scene by its AssetReference.
    If the scene is already loaded it will be unloaded and the new scene will be loaded.
    If the scene is already loading it will wait for the scene to be loaded.
    If the scene is already loaded and the same scene is requested again it will return the same promise that was returned the first time the scene was requested.

    Parameters

    Returns Promise<boolean>

    a promise that resolves to true if the scene was loaded successfully

    const myAssetReference = new AssetReference("scene1.glb");
    sceneSwitcher.switchScene(myAssetReference).then(res => { console.log("Scene loaded", res); });
  • Unload the currently loaded scene.

    Returns Promise<void>

  • Called once per frame during the main update loop. The primary location for frame-based game logic.

    Returns void