SceneManager.activeSceneChanged
Other Versions
Leave feedback
Parameters
value Previous active scene and the new active scene.
Description
Subscribe to this event to get notified when the active Scene has changed.
This script added to activeSceneChanged takes two hidden arguments. These are the replaced
Scene and the next Scene. The arguments are not visible.
In the Editor this event is sent only in Play mode (not in Edit mode). If the event is needed for Edit
mode then use EditorSceneManager.activeSceneChanged.
// SceneManager.activeSceneChanged
//
// This example configures Scene1 to wait for 1.5 seconds before switching
to Scene2.
// Scene1 is the replaced scene; Scene2 is the new loaded scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ScriptExample1 : MonoBehaviour
{
public delegate void Change();
public static event Change TimeChanged;
public void Start()
{
SceneManager.activeSceneChanged += ChangedActiveScene;
// wait 1.5 seconds before change to Scene2
StartCoroutine(TimeChangedScene());
}
IEnumerator TimeChangedScene()
{
print(Time.time + " seconds");
yield return new WaitForSeconds(1.5f);
print(Time.time + " seconds");
// call the event
TimeChanged();
}
private void ChangedActiveScene(Scene current, Scene next)
{
string currentName = current.name;
if (currentName == null)
{
// Scene1 has been removed
currentName = "Replaced";
}
Debug.Log("Scenes: " + currentName + ", " + next.name);
}
void OnEnable()
{
Debug.Log("OnEnable");
ScriptExample1.TimeChanged += ChangeScene;
}
void ChangeScene()
{
Debug.Log("Changing to Scene2");
SceneManager.LoadScene("Scene2");
Scene scene = SceneManager.GetSceneByName("Scene2");
SceneManager.SetActiveScene(scene);
}
void OnDisable()
{
ScriptExample1.TimeChanged -= ChangeScene;
Debug.Log("OnDisable happened for Scene1");
}
}
ScriptExample2 simply announces that this is the active scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptExample2 : MonoBehaviour
{
void Start()
{
Debug.Log("Script2 starting");
}
}
SceneManager.sceneLoaded
Other Versions
Leave feedback
Description
Add a delegate to this to get notifications when a Scene has loaded.
Rather than being called directly this script code shows use of a delegate. This means
the sceneLoaded value is added into a list of delegates.
In the script example below a number of method calls are shown. Specifically the first labelled
method OnEnable() is used to add OnSceneLoaded (), which is not a Unity provided method.
The SceneManager.sceneLoaded delegate can have any method hooked into it and it is
OnSceneLoaded () here. Finally, OnDisable() is used to remove OnSceneLoaded ()
from SceneManager.sceneLoaded.
using UnityEngine;
using UnityEngine.SceneManagement;
public class ExampleCode : MonoBehaviour
{
// called zero
void Awake()
{
Debug.Log("Awake");
}
// called first
void OnEnable()
{
Debug.Log("OnEnable called");
SceneManager.sceneLoaded += OnSceneLoaded;
}
// called second
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("OnSceneLoaded: " + scene.name);
Debug.Log(mode);
}
// called third
void Start()
{
Debug.Log("Start");
}
// called when the game is terminated
void OnDisable()
{
Debug.Log("OnDisable");
SceneManager.sceneLoaded -= OnSceneLoaded;
}
}

More Related Content

PPTX
Angular 2.0 change detection
PPT
Evolution of asynchrony in (ASP).NET
DOCX
Clubbing zones with angular
KEY
openFrameworks 007 - events
PPTX
Event and Signal Driven Programming Zendcon 2012
PPTX
Micro c lab1(intro to 8051)
DOC
Calculadora
PDF
PROGRAMA DE EJEMPLO
Angular 2.0 change detection
Evolution of asynchrony in (ASP).NET
Clubbing zones with angular
openFrameworks 007 - events
Event and Signal Driven Programming Zendcon 2012
Micro c lab1(intro to 8051)
Calculadora
PROGRAMA DE EJEMPLO

Similar to Scene manager (20)

PDF
A Series of Fortunate Events - PHP Benelux Conference 2015
PDF
Photon Server Deep Dive - View from Implmentation of PhotonWire, Multiplayer ...
PDF
A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
PDF
A Series of Fortunate Events - Symfony Camp Sweden 2014
PDF
Pointer Events Working Group update / TPAC 2023 / Patrick H. Lauke
PPT
CSharp_04_Events-in-C#-introduction-with-examples
PPTX
Robotlegs Extensions
PDF
CH05.pdf
PPT
05 Transactions
ODP
Android App Development - 07 Threading
PDF
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
PPTX
event-handling.pptx
PPTX
Dependency injection using dagger2
ODP
The Next Step in AS3 Framework Evolution
PDF
Useful Tools for Making Video Games - XNA (2008)
PPTX
Omnia App With Samsung Sdk
PDF
Angular 16 – the rise of Signals
PDF
Implementações paralelas
PDF
OpenWebBeans and DeltaSpike at ApacheCon
PDF
Android RuntimePermissionsExtended
A Series of Fortunate Events - PHP Benelux Conference 2015
Photon Server Deep Dive - View from Implmentation of PhotonWire, Multiplayer ...
A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
A Series of Fortunate Events - Symfony Camp Sweden 2014
Pointer Events Working Group update / TPAC 2023 / Patrick H. Lauke
CSharp_04_Events-in-C#-introduction-with-examples
Robotlegs Extensions
CH05.pdf
05 Transactions
Android App Development - 07 Threading
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
event-handling.pptx
Dependency injection using dagger2
The Next Step in AS3 Framework Evolution
Useful Tools for Making Video Games - XNA (2008)
Omnia App With Samsung Sdk
Angular 16 – the rise of Signals
Implementações paralelas
OpenWebBeans and DeltaSpike at ApacheCon
Android RuntimePermissionsExtended
Ad

Recently uploaded (20)

PPTX
8086.pptx microprocessor and microcontroller
PPT
Unit I Preparatory process of dyeing in textiles
PPTX
Bitcoin predictor project presentation
PPTX
URBAN FINANCEnhynhynnnytnynnnynynyynynynyn
PPTX
Introduction to Building Information Modeling
PDF
Govind singh Corporate office interior Portfolio
PPTX
timber basics in structure mechanics (dos)
PPTX
Evolution_of_Computing_Presentation (1).pptx
PPTX
Presentation1.pptxnmnmnmnjhjhkjkjkkjkjjk
PPTX
22CDO02-IMGD-UNIT-I-MOBILE GAME DESIGN PROCESS
PPT
aksharma-dfs.pptgfgfgdfgdgdfgdfgdgdrgdgdgdgdgdgadgdgd
PPTX
22CDH01-V3-UNIT-I INTRODUCITON TO EXTENDED REALITY
PDF
1 Introduction to Networking (06).pdfbsbsbsb
PDF
Timeless Interiors by PEE VEE INTERIORS
PPT
EthicsNotesSTUDENTCOPYfghhnmncssssx sjsjsj
PPTX
Drafting equipment and its care for interior design
PDF
Architecture Design Portfolio- VICTOR OKUTU
PPTX
UNIT III - GRAPHICS AND AUDIO FOR MOBILE
PPTX
Necrosgwjskdnbsjdmdndmkdndndnmdndndkdmdndkdkndmdmis.pptx
PPTX
Presentation.pptx anemia in pregnancy in
8086.pptx microprocessor and microcontroller
Unit I Preparatory process of dyeing in textiles
Bitcoin predictor project presentation
URBAN FINANCEnhynhynnnytnynnnynynyynynynyn
Introduction to Building Information Modeling
Govind singh Corporate office interior Portfolio
timber basics in structure mechanics (dos)
Evolution_of_Computing_Presentation (1).pptx
Presentation1.pptxnmnmnmnjhjhkjkjkkjkjjk
22CDO02-IMGD-UNIT-I-MOBILE GAME DESIGN PROCESS
aksharma-dfs.pptgfgfgdfgdgdfgdfgdgdrgdgdgdgdgdgadgdgd
22CDH01-V3-UNIT-I INTRODUCITON TO EXTENDED REALITY
1 Introduction to Networking (06).pdfbsbsbsb
Timeless Interiors by PEE VEE INTERIORS
EthicsNotesSTUDENTCOPYfghhnmncssssx sjsjsj
Drafting equipment and its care for interior design
Architecture Design Portfolio- VICTOR OKUTU
UNIT III - GRAPHICS AND AUDIO FOR MOBILE
Necrosgwjskdnbsjdmdndmkdndndnmdndndkdmdndkdkndmdmis.pptx
Presentation.pptx anemia in pregnancy in
Ad

Scene manager

  • 1. SceneManager.activeSceneChanged Other Versions Leave feedback Parameters value Previous active scene and the new active scene. Description Subscribe to this event to get notified when the active Scene has changed. This script added to activeSceneChanged takes two hidden arguments. These are the replaced Scene and the next Scene. The arguments are not visible. In the Editor this event is sent only in Play mode (not in Edit mode). If the event is needed for Edit mode then use EditorSceneManager.activeSceneChanged. // SceneManager.activeSceneChanged // // This example configures Scene1 to wait for 1.5 seconds before switching to Scene2. // Scene1 is the replaced scene; Scene2 is the new loaded scene. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class ScriptExample1 : MonoBehaviour { public delegate void Change();
  • 2. public static event Change TimeChanged; public void Start() { SceneManager.activeSceneChanged += ChangedActiveScene; // wait 1.5 seconds before change to Scene2 StartCoroutine(TimeChangedScene()); } IEnumerator TimeChangedScene() { print(Time.time + " seconds"); yield return new WaitForSeconds(1.5f); print(Time.time + " seconds"); // call the event TimeChanged(); } private void ChangedActiveScene(Scene current, Scene next) { string currentName = current.name; if (currentName == null)
  • 3. { // Scene1 has been removed currentName = "Replaced"; } Debug.Log("Scenes: " + currentName + ", " + next.name); } void OnEnable() { Debug.Log("OnEnable"); ScriptExample1.TimeChanged += ChangeScene; } void ChangeScene() { Debug.Log("Changing to Scene2"); SceneManager.LoadScene("Scene2"); Scene scene = SceneManager.GetSceneByName("Scene2"); SceneManager.SetActiveScene(scene); } void OnDisable()
  • 4. { ScriptExample1.TimeChanged -= ChangeScene; Debug.Log("OnDisable happened for Scene1"); } } ScriptExample2 simply announces that this is the active scene. using System.Collections; using System.Collections.Generic; using UnityEngine; public class ScriptExample2 : MonoBehaviour { void Start() { Debug.Log("Script2 starting"); } } SceneManager.sceneLoaded
  • 5. Other Versions Leave feedback Description Add a delegate to this to get notifications when a Scene has loaded. Rather than being called directly this script code shows use of a delegate. This means the sceneLoaded value is added into a list of delegates. In the script example below a number of method calls are shown. Specifically the first labelled method OnEnable() is used to add OnSceneLoaded (), which is not a Unity provided method. The SceneManager.sceneLoaded delegate can have any method hooked into it and it is OnSceneLoaded () here. Finally, OnDisable() is used to remove OnSceneLoaded () from SceneManager.sceneLoaded. using UnityEngine; using UnityEngine.SceneManagement; public class ExampleCode : MonoBehaviour { // called zero void Awake() { Debug.Log("Awake"); } // called first void OnEnable() { Debug.Log("OnEnable called");
  • 6. SceneManager.sceneLoaded += OnSceneLoaded; } // called second void OnSceneLoaded(Scene scene, LoadSceneMode mode) { Debug.Log("OnSceneLoaded: " + scene.name); Debug.Log(mode); } // called third void Start() { Debug.Log("Start"); } // called when the game is terminated void OnDisable() { Debug.Log("OnDisable"); SceneManager.sceneLoaded -= OnSceneLoaded;
  • 7. } }