Unity 5上OnLevelWasLoaded在哪里?
在这个新的统一版本中,我认为使用了SceneManager。 但我无法找到如何在水平上加载SceneManager。
旧方式:
void OnLevelWasLoaded(){ // do something }
当我尝试旧方法时,我得到了这个:
在MusicManager上找到OnLevelWasLoaded此消息已被弃用,将在更高版本的Unity中删除。 将一个委托添加到SceneManager.sceneLoaded,以在场景加载完成后获取通知
我不知道如何使用
SceneManager.sceneLoaded();
不知道要通过什么……
你必须将sceneLoaded
作为一个事件。
在Start()
或Awake()
函数中注册sceneLoaded
事件。
SceneManager.sceneLoaded += this.OnLoadCallback;
然后在加载场景时调用OnLoadCallback
函数。
OnLoadCallback
函数签名:
void OnLoadCallback(Scene scene, LoadSceneMode sceneMode) { }
这篇文章很好地解释了这一点 :
旧方式:
void OnLevelWasLoaded (int level) { //Do Something }
新方式:
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); }
请注意,’OnLevelFinishedLoading’是我编写的名称。 您可以随意命名您的方法。
您在
OnEnable
和OnDisable
函数中看到的是委托订阅。 这只是意味着我们正在设置我们选择的函数(在本例中为OnLevelFinishedLoading
)来监听SceneManager
以进行级别更改。另请注意,由于此委托有两个参数(
Scene
和SceneMode
),因此您必须包含这两个参数 – 即使您不打算在函数中使用该信息。