In this new unity version, I think SceneManager is used. But I cant find how to do on level was loaded with SceneManager.
The old way :
void OnLevelWasLoaded(){
// do something
}
When I try the old way I get this:
OnLevelWasLoaded was found on MusicManager This message has been deprecated and will be removed in a later version of Unity. Add a delegate to SceneManager.sceneLoaded instead to get notifications after scene loading has completed
I dont know how to use
SceneManager.sceneLoaded();
Dont know what to pass...
You have to sceneLoaded
as an event.
Register sceneLoaded
event in the Start()
or Awake()
function.
SceneManager.sceneLoaded += this.OnLoadCallback;
The OnLoadCallback
function will then be called when scene is loaded.
The OnLoadCallback
function signature:
void OnLoadCallback(Scene scene, LoadSceneMode sceneMode)
{
}
This is well explained in this post:
The old way:
void OnLevelWasLoaded (int level) { //Do Something }
The new way:
using UnityEngine.SceneManagement; void OnEnable() { //Tell our 'OnLevelFinishedLoading' function to start listening for a scene change as soon as this script is enabled. SceneManager.sceneLoaded += OnLevelFinishedLoading; } void OnDisable() { //Tell our 'OnLevelFinishedLoading' function to stop listening for a scene change as soon as this script is disabled. //Remember to always have an unsubscription for every delegate you subscribe to! SceneManager.sceneLoaded -= OnLevelFinishedLoading; } void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode) { Debug.Log("Level Loaded"); Debug.Log(scene.name); Debug.Log(mode); }
Note that 'OnLevelFinishedLoading' is a name that I've made up. You can name your method whatever you like.
What you're seeing in the
OnEnable
andOnDisable
functions are delegate subscriptions. This simply means that we are setting a function of our choice (in this case,OnLevelFinishedLoading
) to listen to theSceneManager
for a level change.Also note, that since this delegate has two parameters (
Scene
andSceneMode
), that you must include those two parameters as well - even if you don't plan on using that information in your function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With