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. Dismiss Notice

Question Pre/post-processing subscenes

Discussion in 'Entity Component System' started by apkdev, Aug 9, 2021.

  1. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    Has anyone tried post-processing subscenes? I'd like to clean my scene up by deleting some GameObjects before conversion, or at least post-process the entities generated from authoring components. No matter how I tried, I got an error if GameObjects/Entities were deleted at any step during conversion.
     
    JesOb likes this.
  2. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    334
    What you need (if you using subscenes only at start) is to manually load your system and then add post-processing systems and update world until work is done. The real problem in my opinion is to load subscene, because required systems are private. In this thread i've got recomendations to use reflection with name comparison to get private systems i need.
     
    apkdev likes this.
  3. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    Do you mean at runtime? I would like to do this at authoring time to make the subscenes lighter and faster to load.
     
  4. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    334
    I mean at initialization phase of the game
     
  5. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    I already do stuff like that at runtime when subscenes load (eg. spawning based on entity data), but I want to avoid having to do more runtime stuff because that slows down loading :D Also, my built subscene files are getting a bit massive, and I wonder if there's a way I could throw some of that data out. Like static entities that ended up with the Disabled tag.

    I also think being able to post-process the scene (eg. like the good old PostProcessSceneAttribute callback) would improve procedural generation capabilities. I had a hard time with this previously.

    (I could clean up the scene manually, but it would mean flattening of prefabs and overall reduced editability.)
     
  6. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    Quick update - can't test this because I'm taking a break for a couple of weeks, but maybe this is useful to somebody in the interim.

    Based on this post, stripping down scenes during conversion should be possible to some extent. I am assuming CMarastoni is referring to GameObjectAfterConversionGroup. I didn't realize this system existed (only tried GameObjectConversionGroup), and it has a few more friends:

    Code (CSharp):
    1. public class GameObjectDeclareReferencedObjectsGroup : ComponentSystemGroup {}
    2. public class GameObjectBeforeConversionGroup : ComponentSystemGroup {}
    3. public class GameObjectConversionGroup : ComponentSystemGroup {}
    4. public class GameObjectAfterConversionGroup : ComponentSystemGroup {}
    5. public class GameObjectExportGroup : ComponentSystemGroup {}
    When I get back I'll try to figure out if any of these are a good starting point.
     
  7. Zec_

    Zec_

    Joined:
    Feb 9, 2017
    Posts:
    148
    You might be interested in systems that runs in WorldSystemFilterFlags.EntitySceneOptimizations. They are executed in the world where the SubScenes are converted to, after conversion but before they are saved to disk. You do not have authoring components available there though, it's executed after all the conversion has been done. There are various internal entities systems you can look at if you want an example, just search the Entities package for systems tagged with those flags.

    Alternatively there's also the WorldSystemFilterFlags.ProcessAfterLoad systems that you can use to process an SubScene during loading, before it's entities are loaded into your world. It's used for things such as instantiating SubScenes and modifying them, such as translating one of the scenes in the world. I wrote a guide on that back in 2020 that seems to still hold up:
    https://forum.unity.com/threads/generate-sub-scenes-programmatically.868984/#post-6280625
     
    apkdev, Lukas_Kastern and WAYNGames like this.