Search Unity

Question How to guarantee all subscenes are converted before entering play mode?

Discussion in 'Entity Component System' started by PublicEnumE, May 11, 2021.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    During conversion, I collect some metadata about each subscene (like the counts of certain types of objects, etc.), and record it in a ScriptableObject. I then write that ScriptableObject to disk. This SO is reloaded, updated, and saved again when each new subscene is converted, so it doesn't need to access all subscenes at once. It only matters that all subscenes have been converted before the game is run, so that it contains data from all of them.

    This works fine in builds, because all subscenes are (seemingly) converted during the build process. But it usually doesn't work in Editor play mode, since there's no guarantee all subscenes will have been converted before I press play (and then load my ScriptableObject to use it in game).

    In fact, it seems like the Editor takes a JIT approach to converting subscenes in the Editor: They're usually not converted until the moment you load them, in game. This is different from what the documentation and Unity talks have said about subscene conversion: that they are scheduled to be converted on a background thread by the Editor, the moment you close them.

    At some point, that approach must have changed, because that's not happening.

    So, we're left with a question: How can I guarantee that all subscenes have actually undergone conversion before I enter play mode in the editor?

    Otherwise we're stuck making a new build to test every change.
     
    Last edited: May 11, 2021
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Don't have a solution for you but

    My subscenes start converting as soon as I close them. You can see it in the background tasks window. No guarantee it'll be complete before entering play mode though.
     
    PublicEnumE likes this.
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    I think a reactive approach is probably the most reliable. And not just for entity prefab loading but in general. It's the approach Unity seems to be taking themselves also, so my guess is that reactive would be their answer here. Setup queries for what you are interested in and tie them to say a loaded/not loaded state of some type. With a query something like this:

    Code (csharp):
    1.  
    2. .WithEntityQueryOptions(EntityQueryOptions.IncludePrefab)
    3. .WithoutBurst()
    4. .WithStructuralChanges()
    5.  
    Put these queries in a method that doesn't run once loaded.

    Might be more elegant approaches query wise, and naturally you can abstract this out a bit if that's worth it.
     
    PublicEnumE likes this.