- Home /
How to ensure that an additively loaded scene is active before it becomes interactive?
I'm using the following code to load a scene additively that's supposed to become the active scene:
SceneManager.LoadScene(scene, LoadSceneMode.Additive);
yield return null;
SceneManager.SetActiveScene(SceneManager.GetSceneByName(scene));
(scene
is a string containing the name of the scene)
problem is, the scene can contain buttons that should only be clickable once the scene is actually the active scene. the way it is, the scene starts inactive and then becomes active. i want the scene to only show up when it's already active.
any way to do that?
Have you looked at the Scene $$anonymous$$anager class? This might help if you want to see when a scene was loaded.
edit:
have tested the delegate. it appears that unity is actually calling the sceneLoaded
-delegate before the scene is loaded. hard to believe, but in this code, bla
turns out to be false
:
private void OnSceneLoaded(Scene scene, LoadScene$$anonymous$$ode mode) {
Debug.Log("OnSceneLoaded: " + scene.name + " FrameCount: " + Time.frameCount);
bool bla = Scene$$anonymous$$anager.SetActiveScene(scene);
Debug.Log(bla);
}
according to the documentaiton for SetActiveScene
, it returns false
when the scene isnt loaded yet.
Answer by Orolo · Oct 31, 2016 at 03:16 PM
not super happy with it, but i've now done it this way:
changed my scene-loading-script from the three lines in the question to:
SceneManager.LoadScene(scene, LoadSceneMode.Additive);
yield return new WaitUntil(() => SceneManager.GetActiveScene().name == scene);
(the yield return because callers need to be able to wait until the loading is complete)
then made following script:
using UnityEngine;
using UnityEngine.SceneManagement;
public class ActivateScene : MonoBehaviour {
void Start() {
bool wasLoaded = SceneManager.SetActiveScene(gameObject.scene);
Debug.Assert(wasLoaded);
}
}
had to use Start
because Awake
and OnEnable
are both called before scene is considered loaded.
added ActivateScene
to Script Execution Order settings and put it before Default Time.
then of course, added an ActivateScene
-component to every scene that needs to be auto-activated.
if anyone knows a better way, please let me know.
Your answer
Follow this Question
Related Questions
,Enums not being passed between scripts properly 1 Answer
How can I have several scenes running at the same time? 1 Answer
Load scene in editor playmode synchronously 1 Answer
SceneManager.GetAllScenes() only returns the current scene 3 Answers
Re-loading a scene but on the background older scenes are displayed 1 Answer