Make SerializeWorld skip serializing archetype/entity. (+SubScene issue)

Is there any way to make SerializeUtility.SerializeWorld skip serializing an entity or archetype when Serializing? I have some entities I’d rather spawn in manually when the world is created than having them be saved and loaded, and I copying just the entities I want to keep over to a new world sounds a bit strange.

Additionally, is there any way to prevent unity from creating the SubScene entity when converting a subscene? For some reason there’s an entity for each sub-scene, despite them only existing to load in a bunch of entities for testing. These have SceneReference components, which end up as referenced objects when serializing the world, which means I’d have to somehow keep track of them when deserializing too. I tried deleting them, but then unity has the following error:

ArgumentException: DestroyEntity(EntityQuery query) is destroying entity Entity(62:1) 'Scene: StressTestBuildings' which contains a LinkedEntityGroup and the entity Entity(1167:1) 'SceneSection: StressTestBuildings (0)' in that group is not included in the query. If you want to destroy entities using a query all linked entities must be contained in the query..
Unity.Entities.EntityComponentStore.ThrowDestroyEntityErrorFancy (Unity.Entities.Entity errorEntity, Unity.Entities.Entity errorReferencedEntity) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/EntityComponentStoreDebug.cs:623)
Unity.Entities.EntityComponentStore.ThrowDestroyEntityError (Unity.Entities.Entity errorEntity, Unity.Entities.Entity errorReferencedEntity) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/EntityComponentStoreDebug.cs:613)
Unity.Entities.EntityDataAccess.DestroyEntitiesInChunksDuringStructuralChange (Unity.Entities.EntityQueryImpl* queryImpl, Unity.Entities.SystemHandle& originSystem) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/EntityDataAccess.cs:578)
Unity.Entities.EntityManager.DestroyEntitiesInChunks (Unity.Entities.EntityQueryImpl* queryImpl) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/EntityManager.cs:4967)
Unity.Entities.EntityManager.DestroyEntity (Unity.Entities.EntityQuery entityQuery) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/EntityManager.cs:3479)
LifeBelow.Workers.DestroySubsceneReferencesSystem.OnUpdate (Unity.Entities.SystemState& state) (at Assets/Scripts/SaveLoad/DestroySubsceneReferencesSystem.cs:25)
LifeBelow.Workers.DestroySubsceneReferencesSystem.__codegen__OnUpdate (System.IntPtr self, System.IntPtr state) (at <9f3b6308c77b4db0981fb3ce2a8a03ba>:0)
Unity.Entities.SystemBaseRegistry.ForwardToManaged (System.IntPtr delegateIntPtr, Unity.Entities.SystemState* systemState, System.Void* systemPointer) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/SystemBaseRegistry.cs:371)
Unity.Entities.SystemBaseRegistry.CallForwardingFunction (Unity.Entities.SystemState* systemState, Unity.Entities.UnmanagedSystemFunctionType functionType) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/SystemBaseRegistry.cs:340)
Unity.Entities.SystemBaseRegistry.CallOnUpdate (Unity.Entities.SystemState* systemState) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/SystemBaseRegistry.cs:383)
Unity.Entities.WorldUnmanagedImpl+UnmanagedUpdate_00001529$BurstDirectCall.Invoke (System.Void* pSystemState) (at <086c5bef13af42a49beed06291b6c03d>:0)
Unity.Entities.WorldUnmanagedImpl.UnmanagedUpdate (System.Void* pSystemState) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/WorldUnmanaged.cs:828)
Unity.Entities.WorldUnmanagedImpl.UpdateSystem (Unity.Entities.SystemHandle sh) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/WorldUnmanaged.cs:913)
Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/ComponentSystemGroup.cs:729)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/Stubs/Unity/Debug.cs:19)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/ComponentSystemGroup.cs:740)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/ComponentSystemGroup.cs:699)
Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/SystemBase.cs:418)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/ComponentSystemGroup.cs:735)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/ComponentSystemGroup.cs:693)
Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/SystemBase.cs:418)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.8/Unity.Entities/ScriptBehaviourUpdateOrder.cs:526)

Here’s what the entities look like in the yaml serialized world:

Archetype:
    name: "  - Unity.Entities.Entity  - Unity.Entities.RequestSceneLoaded  - Unity.Entities.SceneReference  - Unity.Scenes.LiveConversionPatcher+LiveConvertedSceneCleanup  - Unity.Scenes.ResolvedSectionEntity [Buffer]  - Unity.Scenes.SubScene  - Unity.Scenes.DisableSceneResolveAndLoad  - Unity.Entities.Simulate"
    ChunkData:
      Header:
        metaChunkEntity: {index: 0, version: 0}
        Capacity: 84
        Count: 2
      ComponentDataCollection:
        ComponentData:
          info: {Type: Entity, SizeInChunk: 8}
          Entities:
            Entity(62:1): {Index: "62", Version: "1"}
            Entity(63:1): {Index: "63", Version: "1"}
        ComponentData:
          info: {Type: RequestSceneLoaded, SizeInChunk: 4}
          Entities:
            Entity(62:1): {LoadFlags: "BlockOnImport"}
            Entity(63:1): {LoadFlags: "BlockOnImport"}
        ComponentData:
          info: {Type: SceneReference, SizeInChunk: 16}
          Entities:
            Entity(62:1): {SceneGUID: "ff1f8c57174caac4885384956747f0f0"}
            Entity(63:1): {SceneGUID: "01e81325d69b9ba47a278096ccf21c17"}
        ComponentData:
          info: {Type: LiveConvertedSceneCleanup, SizeInChunk: 16}
          Entities:
            Entity(62:1): {Scene: "ff1f8c57174caac4885384956747f0f0"}
            Entity(63:1): {Scene: "01e81325d69b9ba47a278096ccf21c17"}
        ComponentData:
          info: {Type: ResolvedSectionEntity, SizeInChunk: 144}
          Entities:
            Entity(62:1):
              Length: 1 Capacity: 16
              0000: {SectionEntity: "Entity(66:1)"}
            Entity(63:1):
              Length: 1 Capacity: 16
              0000: {SectionEntity: "Entity(29070:1)"}

The best way to handle this is to create a separate World for your gameplay stuff and serialize that instead so that you don’t get the extra stuff from subscenes. Although, I have no experience of doing this yet.

I figured out that the extra stuff from subscenes can be removed by just unloading them without retaining metadata:

            var world = World.DefaultGameObjectInjectionWorld;

            var subscenes = Object.FindObjectsOfType<SubScene>();
            if (subscenes.Length > 0)
            {
                Debug.Log($"Unloading {subscenes.Length} subscenes before saving");
                foreach (var subscene in subscenes)
                {
                    var subsceneEntity = SceneSystem.GetSceneEntity(world.Unmanaged, subscene.SceneGUID);
                    SceneSystem.UnloadScene(world.Unmanaged, subsceneEntity, SceneSystem.UnloadParameters.DestroyMetaEntities);
                }
            }

However, this unloaded the entities from the subscenes too, which solves my other problem. I can just make whatever I want to not save be in subscenes, and have whatever I want to keep be spawned in. Only makes it so I have to be careful not to have any direct references to prefabs.

EDIT: Nevermind, turns out instantiating a prefab spawns it inside the subscene, and thus makes it unload when I unload the subscene. Manually created entities don’t do this, but I might as well just keep a reference to the scene at this point, and just not allow saving for test scenes.