Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unload subscene

Discussion in 'Entity Component System' started by Holyren, Jan 7, 2022.

  1. Holyren

    Holyren

    Joined:
    Jul 8, 2017
    Posts:
    35
    I am currently loading and unloading a subscene via script. Oddly enough, the first time I try to load and then unload, it works. However, after that the unloading stops working. As in I can load the subscene once or even multiple times but the unload does not work. No errors are given.

    I did not find any example of how to implement unloading, I wrote my script as following:

    Code (CSharp):
    1.     public void LoadSubScene()
    2.     {
    3.         var loadParameters = new SceneSystem.LoadParameters
    4.         {
    5.             Flags = SceneLoadFlags.NewInstance
    6.         };
    7.         levelTest = test.SceneGUID;
    8.         sceneSystem.LoadSceneAsync(levelTest, loadParameters);
    9.     }
    10.  
    11.     public void UnLoadSubScene()
    12.     {
    13.         SceneSystem.UnloadParameters unloadParams = SceneSystem.UnloadParameters.Default;
    14.  
    15.         sceneSystem.UnloadScene(levelTest, unloadParams);
    16.     }
     
  2. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    127
    I had exactly the same issue while trying to instantiane many subscene copies. In your case, however, it doesn't sound like you need multiple copies, so why NewInstance?
     
  3. Holyren

    Holyren

    Joined:
    Jul 8, 2017
    Posts:
    35
    Thanks, that solved it for me.
     
  4. Zec

    Zec

    Joined:
    Jan 3, 2012
    Posts:
    10
    Good to hear that Holyren got it solved!

    I'm not sure if you (WildMaN) still have the issue where you attempted to unload your instances, but if you haven't solved it there is an UnloadScene overload where you need pass the scene instance entity (the entity that is returned from the LoadSceneAsync call) for the instance you want to unload. It works all fine for me.
     
  5. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    127
    Hey, I thought about that, but the return type of LoadSceneAsync is AsyncOperation. I just checked it again and I do not see how I can get a scene Entity. And trying to unload the root entity of a SubScene didn't work, just as I mentioned in the original comment.
     
  6. Zec

    Zec

    Joined:
    Jan 3, 2012
    Posts:
    10
    AsyncOperation? It sounds to me like you're trying to load it via SceneManager.LoadSceneAsync?

    SubScenes are loaded via SceneSystem.LoadSceneAsync, where all overloads either return an Entity or take an entity (if you instantiate a request manually). That's atleast how it is in Entities 0.17 which is the version I use.

    Here's a sample from inside SceneSystem.
    upload_2022-1-14_15-32-16.png

    Edit: Here's a link to an older forum post where I played around with this in 0.13 and explained the process of loading, instantiating and offsetting SubScene instances. Feel free to take a look! It seems like the post still holds up despite it being for an older version.
    https://forum.unity.com/threads/generate-sub-scenes-programmatically.868984/#post-6280625
     
    JJTWGames likes this.
  7. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    127
    Best
    Naming
    Ever

    I had to re-read your comment 3 times before I spotted the issue. Of course I'm using the system and of course I was fooled by manager's documentation. Now it all makes perfect sense, ty!
     
  8. contact_polymesh

    contact_polymesh

    Joined:
    May 9, 2019
    Posts:
    3
    I'm facing the same issue. How did you solve it?
    my subscene load and unload in the editor. but in the android device it loads fine but it don't unload.
     
  9. Holyren

    Holyren

    Joined:
    Jul 8, 2017
    Posts:
    35
    I solved it by removing the new instance flag when loading subscene; note that I used a much older version.
    Code (CSharp):
    1.     private void LoadSubScene(SubScene subScene)
    2.     {
    3.         var loadedSubScene = SceneSystem.LoadSceneAsync(worldUnmanaged, subScene.SceneGUID);
    4.     }
    I assume this is unrelated to your issue, as you mention it working in the Editor. Therefore, I recommend you check Android Logcat for any potential errors.
     
  10. contact_polymesh

    contact_polymesh

    Joined:
    May 9, 2019
    Posts:
    3
    yeah i found out the issue.
    I have 2 Scenes. MainMenu Scene and Gameplay Scene and Gameplay Scene Contains Subscenes.
    In the android Classic Build. When I set the Gameplay Scene as AutoLoad. Everything Works Fine and my subscenes load and unload on android as expected.
    But When I Set MainMenu as autoload and load the Gameplay Scene via Script:
    SceneManager.LoadScene("Gameplay");
    My Gameplay Scene and Subscenes load as expected but they do not unload.
    its been 3 weeks and i couldn't find a solution.