Since OnLevelWasLoaded is deprecated (in 5.4.0b15) What should be use instead?
3 Answers
3Since a reliable coding example hasn’t come up in the first few Google searches, I thought I’d post the new suggested implementation here.
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 and OnDisable functions are delegate subscriptions. This simply means that we are setting a function of our choice (in this case, ‘OnLevelFinishedLoading’) to listen to the SceneManager for a level change.
Also note, that since this delegate has two parameters (Scene and SceneMode), that you must include those two parameters as well - even if you don’t plan on using that information in your function.
The problem is that this doesn't actually work as the timing of the new event is different to OnLevelWasLoaded. When I tested this implementation I found that the scene isn't actually fully loaded when the new event fires (which it is when OnLevelWasLoaded is called) and as such this method is not reliable. We have a unity forum thread open on this which unity are looking at to determine what should be done to resolve this problem ..... http://forum.unity3d.com/threads/important-function-deprecated-what-is-the-new-alternative.424746/
– catfinkI had to use UnityEngine.SceneManagement.Scene, because Unity apparently has a class Scene that wasn't what I was looking for, and it was causing parameters match error. It took a while to figure it out, so I'm leaving it here if someone encounters the same problem.
– TuitaioInstead of using Onlevelwasloaded(), add a delegate to SceneManager.sceneLoaded to get notifications after scene loading has completed.
using UnityEngine.SceneManagement;
void OnEnable() {
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnDisable() {
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
//do stuff
}
Hello, I have Unity 5.5.1 (64bit) and Vuforia to try to augment things. In “Console – Clear on play”, I always get this message: "OnLevelWasLoaded was found on VuforiaAbstractBehaviour 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" How do I delegate to SceneManager.sceneLoaded? I would like to get the notifications after I press on the play button. For example, whether my webcam can pick up the image target or not.
– wilbertllama@agiro Did this answer help you? If so, would you be so kind as to click the "Accept" button above to accept the answer. Thank you!
– TBruceThe SceneManager class has events you can hook into for scene changes/loading/etc.
Specifically, the SceneManager.sceneLoaded event.
Note that these aren’t in the docs for 5.3 and were added in 5.4 seemingly.
In JS, I just used function OnLevelFinishedLoading () { //do stuff } instead of OnLevelWasLoaded. It seems to be working fine for what I used it for.
– psychentist