How to get Scene Switcher Callback

import { Animator, AssetReference, Behaviour, EventList, GameObject, ISceneEventListener, WaitForSeconds, delay, serializable } from “@needle-tools/engine”;
import { Euler, Object3D, Vector3 } from “three”;

export class SceneLoader extends Behaviour implements ISceneEventListener {

@serializable(EventList)
onInitialSceneLoaded?: EventList;

public async sceneOpened(): Promise {
console.log(“Scene has been opened”);
// Any async operations can go here…
this.onInitialSceneLoaded?.invoke()

}

public async sceneClosing(): Promise {
console.log(“Scene is closing”);
// Any async operations can go here…
}

}
I have attached this script to root of the scene object still i don’t get console message on scene opened. Please help on this issue

Original Post on Discord

by user 1141289362336784394

Hi, can you show a screenshot of your scene in Unity showing the SceneLoader component and hierarchy?

yes one sec

by user 1141289362336784394

by user 1141289362336784394

This scene is loaded by a SceneSwitcher?

No this is initial scene

by user 1141289362336784394

Then you can just use the Awake method for example. ISceneEventListener is currently only called by the SceneSwitcher

awake or start would both work - I think start would be better/preferred to not call methods on other components before their own awake method has been called

If the scene loader is attached to the loaded scene will it work. I just need to know how to use IsceneEventListener

by user 1141289362336784394

thanks🙂

by user 1141289362336784394

It will work if you have multiple scenes and you’re using the SceneSwitcher component to load / unload them. Only the SceneSwitcher calls those methods right now.

Let me know if start or awake works for you :slightly_smiling_face:

OH actually I am using SceneSwitcher to load another scene named lobby from this scene. I want to have callback when lobby is loaded to this scene.

by user 1141289362336784394

Then you could also just use the start in one of your scripts in the lobby scene

Or add the SceneLoader component you made to the Root gameobject of the Loading scene - but I think it might not yet work in this case (when you export a scene) because it currently only checks one root object. I’ll have to test and fix this :slightly_smiling_face:

But for your usecase: start should be enough :slightly_smiling_face:

I don’t want to traverse over all the scene objects to get the Player which is in initial scene that’s why i wanted a callback.

by user 1141289362336784394

Can you explain a bit more what your goal is?

I dont think you have to traverse your scene here

I want to trigger player animation when lobby scene is fully loaded. Player object is in main scene (inital).

by user 1141289362336784394

Got it. Assuming you have only one player object in the main scene you can add a script to your player roughly like so:

export class Player extends Behaviour
{
   static players: Player[] = [];
    static onLobbyLoaded() {
         foreach(const pl of players) { 
            // call a method on the player or trigger the animator on the player e.g. pl.gameObject.getComponent(Animator)?.setTrigger("lobbyLoaded");
          }
     }

    awake() { Player.players.push(this) }
    onDestroy() { Players.players.splice(Players.players.indexOf(this)) }
}

and then your lobby scene could have a script like:

export class Lobby extends Behaviour { 
  start() { Player.onLobbyLoaded(); } 
}

Nice I will try this and let you know. Thanks🙂

by user 1141289362336784394