- Home /
How to.. "Add a delegate to this to get notifications"..
HI All, Am using the 5.4 beta. (Just got b11). In the SceneManagenment, there are few events
activeSceneChanged
sceneLoaded
sceneUnloaded
Their descriptions says like.. "Add a delegate to this to get notifications when the active scene has changed"
Have no ideas how i can do that. Can someone kindly provide an example?
For loading, since I use Load Async, I could do it in a coroutine like yield return SceneManager.LoadSceneAsync(...) But the other two.. How do i get a notification, or wait in a coroutine?
Thanks
Answer by Bunny83 · Mar 24, 2016 at 02:33 AM
I don't have 5.4 beta and it's documentation isn't online, so i have no idea how the signature of those events look like. SceneManagenment is a namespace and a namespace can't contain events. Is it possible that those events are inside the SceneManager class?
If those events don't have any parameters you can simply do:
void Awake()
{
SceneManager.activeSceneChanged += MyMethod; // subscribe
}
void OnDestroy()
{
SceneManager.activeSceneChanged -= MyMethod; // unsubscribe
}
void MyMethod()
{
Debug.Log("active scene changed");
}
However if the event takes a parameter (a Scene object for example) you have to declare a method that matches the signature of the event type.
Again, this is based purely on speculations since i don't have the beta version.
Thank You. That is what I needed. BTW, 5.4 beta documentation is online.. http://docs.unity3d.com/540/Documentation/ScriptReference/index.html But I believe it is incomplete. There is absolutely no details on these events. But I could get the parameter from the error messages... For instance, UnityEngine.Scene$$anonymous$$anagement.sceneLoaded(UnityEngine.Scene$$anonymous$$anagement.Scene, UnityEngine.Scene$$anonymous$$anagement.LoadScene$$anonymous$$ode) But I got What I Wanted.
To elaborate on this if anybody else finds this question: From introspecting UnityEngine.dll, I found out the types of these events:
UnityAction<Scene, Scene> activeSceneChanged;
UnityAction<Scene, LoadScene$$anonymous$$ode> sceneLoaded;
UnityAction<Scene> sceneUnloaded;
The UnityAction-Type behaves like a regular C# delegate, so you can subscribe as @Bunny83 showed. However, the function signature needs to be a tad different. The type arguments to the UnityAction describe the argument types that subscribing functions need to expect. For example, to subscribe to activeSceneChanged, you would write:
void Awake()
{
Scene$$anonymous$$anager.activeSceneChanged += $$anonymous$$y$$anonymous$$ethod;
}
void $$anonymous$$y$$anonymous$$ethod(Scene previousScene, Scene newScene)
{
Debug.Log("active scene changed");
}
Answer by tkamruzzaman · Nov 08, 2016 at 03:56 AM
using UnityEngine.SceneManagement;
void OnEnable() {
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnDisable() {
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
//do stuff
}
Answer by stalematestudio · Nov 16, 2016 at 10:54 PM
this is just a useless reacharound injected for no other purpose than to break code and caries no benefit
I think they're trying to decouple everything that has to do with scene management from the core of the engine and keep it all under the Scene$$anonymous$$anagement class.
Your answer

Follow this Question
Related Questions
How to teleport to different scenes? 2 Answers
objects not reappearing 2 Answers
click gameobject, go to the next scene? 2 Answers
How to put seperate scenes together into one game 1 Answer
NGUI Button some how unclickable 3 Answers