Parameters

valueUse a subscription of either a UnityAction<Scene> or a method that takes a Scene type argument.

Description

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

此脚本代码没有直接调用事件,而是显示了使用委托的情形。这表示 sceneUnloaded 值将添加到委托列表中。

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.

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SceneLoaded2 : MonoBehaviour { public void Start() { Debug.Log("SceneLoaded2"); } }