Search Unity

Any way to load a scene additively + async, and start its contained objects in a disabled state?

Discussion in 'Editor & General Support' started by BenHymers, Apr 20, 2016.

  1. BenHymers

    BenHymers

    Joined:
    Dec 16, 2014
    Posts:
    30
    I want to load a scene additively and asynchronously, and have its game objects start off disabled, and to not have OnEnabled called on them.

    The scene I want to load contains one root game object which is enabled.

    I have tried this code but all objects still get an OnEnabled call:

    Code (csharp):
    1.  
    2. IEnumerator LoadScene()
    3. {
    4.     var asyncOp = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync( sceneName, UnityEngine.SceneManagement.LoadSceneMode.Additive );
    5.     yield return asyncOp;
    6.  
    7.     var scene = UnityEngine.SceneManagement.SceneManager.GetSceneByName( sceneName );
    8.     var roots = scene.GetRootGameObjects();
    9.     var sceneGameObject = roots[ 0 ];
    10.      
    11.     sceneGameObject.SetActive( false );
    12. }
    13.  
    I've also tried using asyncOp.allowSceneActivation = false, waiting for progress to be 0.9 (then waiting a bit longer just in case), and disabling objects then, but I can't access the root game objects because the scene isn't fully loaded. It's only flagged as fully loaded once the objects have been enabled.

    Is there a way to achieve what I want? As far as I can see, the only way is to save the objects in the scenes in question in a disabled state, but that's a bit of a pain for the rest of the team (who will have to remember to disable everything every time they check in), and has implications for other things in our workflow, like automated light baking and so on. I'd much prefer to be able to do this via code!
     
  2. usernameHed

    usernameHed

    Joined:
    Apr 5, 2016
    Posts:
    93
    AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(name, mode);
    asyncOperation.allowSceneActivation = false;
     
  3. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
  4. BenHymers

    BenHymers

    Joined:
    Dec 16, 2014
    Posts:
    30
    Thanks Steen - that's very helpful and will work well!

    For anyone else reading - a feature request has been submitted to allow access to GameObjects between loading a scene and their first Update call, and it has been confirmed by Unity enterprise support that this is currently not possible (in 2020.3 but presumably also 2021.X and 2022.X).
     
  5. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    I don't understand how it's more convenient to force users to implement some custom post processing code rather than adding a simple boolean argument to one or two methods.

    If it's not possible in the engine, that is one thing and would be completely understandable. But how it is inconvenient to let the user decide if the scene starts in a disabled state or not is beyond my comprehension. Let me offer a simple solution.

    If the user double clicks a scene or uses the File.OpenScene menu option, open the scene as normal in an active state. If the scene is opened via code with SceneManager.LoadSceneAsync (or addressables) allow us to specify the active state of the scene. Our code will be setup to handle the state we want to open the scene in.
     
    Last edited: Aug 29, 2022
    HunterAhlquist likes this.
  6. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    @SteenLund Have to bring this up again because I still think it's a worthy feature request. Any chance of this ever being added, or is there some limitation of the engine that makes it impossible?

    Just an override to the existing load methods would be great, so that we can have scenes start disabled via scripting only. Don't touch how scene loaded is handled in the editor and there is no worry about development inconvenience.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,930
    I mean Ben's post pretty much sums it up:
    Seems like a straight engine limitation. Scene objects can't be accessed until the scene is completely integrated into the main thread.

    If it were that simple to 'just provide an overload' then I imagine it would have been a feature long ago
     
  8. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    It's possible you're right, but it's not clear to me if an inability for users to be able to access a game object before the scene is integrated into the main thread would stop Unity from being able to internally set the state of those game objects before they are loaded.

    Personally, my requests doesn't require user access to game objects before integration.