Version: 2017.4
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

SceneManager.sceneUnloaded

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Add a delegate to this to get notifications when a Scene has unloaded

Rather than being called directly this script code shows use of a delegate. This means the sceneUnloaded value is added into a list of delegates.

In the script example below a method call is shown. Specifically a function called OnSceneUnloaded is added to sceneUnloaded. SceneUnloaded is called when the scene it is associated with is closed. At this point SceneUnloaded should be removed from the sceneUnloaded list.

#pragma strict
public class SceneLoaded1 extends MonoBehaviour {
	public function Start() {
		SceneManager.sceneUnloaded += OnSceneUnloaded;
		Debug.Log("Start: SceneLoaded1");
	}
	private function OnSceneUnloaded(current: Scene) {
		Debug.Log("OnSceneUnloaded: " + current);
	}
	function Update() {
		if (Input.GetKey("space")) {
			Debug.Log("Quitting Scene1");
			ChangeScene();
		}
	}
	function ChangeScene() {
		Debug.Log("Changing to Scene2");
		SceneManager.LoadScene("Scene2");
	}
	function OnDestroy() {
		Debug.Log("OnDestroy");
	}
}
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoaded1 : MonoBehaviour { public void Start() { SceneManager.sceneUnloaded += OnSceneUnloaded; Debug.Log("Start: SceneLoaded1"); }

private void OnSceneUnloaded(Scene current) { Debug.Log("OnSceneUnloaded: " + current); }

void Update() { if (Input.GetKey("space")) { Debug.Log("Quitting Scene1"); ChangeScene(); } }

void ChangeScene() { Debug.Log("Changing to Scene2");

SceneManager.LoadScene("Scene2"); }

void OnDestroy() { Debug.Log("OnDestroy"); } }

SceneLoaded2 simply announces that this is the active scene.

#pragma strict
public class SceneLoaded2 extends MonoBehaviour {
	public function Start() {
		Debug.Log("SceneLoaded2");
	}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SceneLoaded2 : MonoBehaviour { public void Start() { Debug.Log("SceneLoaded2"); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961