1 回答
TA貢獻1844條經驗 獲得超8個贊
Start您可以為SceneManager.sceneLoaded添加一個偵聽器,而不是在其中執行此操作
僅在加載初始場景時才執行這些操作,您可以使用它SceneManager.GetActiveScene()來存儲并稍后將初始場景與加載的場景進行比較。
// Store the scene that should trigger start
private Scene scene;
private void Awake()
{
// It is save to remove listeners even if they
// didn't exist so far.
// This makes sure it is added only once
SceneManager.sceneLoaded -= OnsceneLoaded;
// Add the listener to be called when a scene is loaded
SceneManager.sceneLoaded += OnSceneLoaded;
DontDestroyOnLoad(gameObject);
// Store the creating scene as the scene to trigger start
scene = SceneManager.GetActiveScene();
}
private void OnDestroy()
{
// Always clean up your listeners when not needed anymore
SceneManager.sceneLoaded -= OnSceneLoaded;
}
// Listener for sceneLoaded
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// return if not the start calling scene
if(!string.Equals(scene.path, this.scene.path) return;
Debug.Log("Re-Initializing", this);
// do your "Start" stuff here
}
Afaik /我如何理解鏈接中的示例OnSceneLoaded也將在第一個場景中調用,只要您在之前添加回調Start(所以在Awakeor中OnEnable)。
注意我使用Scene.path s 而不是scene.name因為path它總是唯一的(由于操作系統文件系統),而name可能不是。
- 1 回答
- 0 關注
- 121 瀏覽
添加回答
舉報
