Load multiple scene

Can I load multiple scenes together without overwriting or unloading the previous scene?

Hi Saad Baig,

You can load multiple scenes together in Needle Engine without overwriting or unloading the previous scene using the SceneSwitcher component. Here’s a general overview of how you can achieve this:

  1. Use SceneSwitcher Component: This component is designed to dynamically load and unload scenes as needed.

  2. Define Scenes: Add your scenes to the scenes array in the SceneSwitcher.

  3. Load Multiple Scenes: You can load scenes to the scene tree without removing the currently loaded scenes. The scenes loaded will be added as children to the SceneSwitcher GameObject.

Here’s a sample code snippet to illustrate how you might implement this:

import { SceneSwitcher, AssetReference } from "@needle-tools/engine";

export class MySceneManager extends Behaviour {
    @serializable(AssetReference)
    scenesToLoad: AssetReference[] = []; // Assign the scenes via the Unity Inspector

    async awake() {
        // Assuming you have a SceneSwitcher component on the same GameObject
        const sceneSwitcher = this.gameObject.getComponent(SceneSwitcher);
        if (sceneSwitcher) {
            for (const scene of this.scenesToLoad) {
                await sceneSwitcher.addScene(scene);
                console.log(`Loaded scene: ${scene.uri}`);
            }
        }
    }
}

Key Points:

  • Each scene is added as a child of the GameObject with the SceneSwitcher.
  • When a new scene is loaded via the switcher, it does not immediately replace the existing scenes, allowing for multiple scenes to exist together.
  • You can manage the visibility of each scene programmatically as per your requirements.

For more details and examples, you can refer to the Multi Scenes dynamic loading sample on Needle Tools Docs (see the documentation).

Feel free to ask if you have more questions!

1 replies left
thread_tg3sTP1SMN2ixkibnpRRmATO
Reply to this message to continue the conversation

Addscene just adds the scene in the code. I want to open the scene all together in the same place

Hi Saad Baig,

To load multiple scenes together in Needle Engine without overwriting or unloading previous scenes, you can use a custom component to manage this functionality.

You can create a component that loads all desired scenes and adds them to your current scene without removing any existing ones. Here’s a pseudocode outline followed by a code example:

Pseudocode

  1. Create a component that holds an array of scenes to load.
  2. In the awake method, iterate through the scenes array.
  3. For each scene:
    • Load the scene asynchronously.
    • Once loaded, add it to the current scene (make sure to attach it to the desired parent object).
  4. Ensure that the scenes are positioned correctly to avoid overlap, if necessary.

Example Code

import { Behaviour, serializable, AssetReference } from "@needle-tools/engine";

export class LoadMultipleScenes extends Behaviour {
    @serializable(AssetReference)
    scenesToLoad?: AssetReference[];

    async awake() {
        if (!this.scenesToLoad || this.scenesToLoad.length === 0) return;

        for (const sceneAsset of this.scenesToLoad) {
            if (sceneAsset) {
                // Load the scene asynchronously
                const scene = await sceneAsset.loadAssetAsync();

                // Adjust the position if necessary
                scene.position.set(x, y, z); // Set x, y, z as required for each scene
                
                // Add the loaded scene to the current scene
                this.gameObject.add(scene);
            }
        }
    }

    onDestroy(): void {
        // Cleanup loaded scenes if necessary
        if (!this.scenesToLoad) return;
        for (const sceneAsset of this.scenesToLoad) {
            sceneAsset?.unload();
        }
    }
}

Key Points:

  • This approach allows you to load multiple scenes into the same space without unloading the previous ones.
  • You may need to manage the positioning of each scene to avoid collisions manually.

Feel free to ask if you have more questions or need further assistance on this topic!

0 replies left
thread_tg3sTP1SMN2ixkibnpRRmATO
Reply to this message to continue the conversation

This topic was automatically closed 13 days after the last reply. New replies are no longer allowed.