Deleting 300 prefab instances from the scene takes 2h (and counting)

(Unity 2022.3.22)

We have a scene where we placed a bunch of building prefabs to lay out a town. In total, around 320 buildings have been placed into the scene.

Admittedly, each building prefab is fairly complex, because the buildings are made of modular pieces that go through a runtime combine process.

Now I’m selecting all the buildings in the scene hierarchy, and trying to delete them. It’s been running for 2h so far, and still running.

In another test, I found that deleting a single building can take anywhere from 15s to 30s or sometimes more. So the math adds up that deleting 320 buildings, will take hours.

So the question is, what on earth is going on there? Why is a Delete of a prefab instance from the scene so slow? For these questions, I’m not sure there will be a satisfactory answer.

But then I’m wondering, is there some kind of work-around or trick some somebody knows, for deleting things fast in bulk?

Side note - I also tried to delete these via script by doing DestroyImmediate in an editor script. After this script ran for 30 minutes, it was clear that the same issue is present, so I killed the Unity process.

Any random ideas for work-around is greatly appreciated.

1 Like

Very likely there’s some form of recursive event processing happening, which in this case is unnecessary but occurs anyway. Probably serialization of every game object and its components. You may be having tens of thousands of game objects in that scene - I definitely seen this drag the editor to a crawl.

There’s a similar thing happening when you move one of these houses by grabbing its root. Every time you do that, all the child transforms get an event update that their parent transform has changed. Perhaps you even noticed that moving a house in that scene is less responsive than moving a primitive.

This might actually be related to the selection. If your editor script also worked on a selection of objects, try to make it work by finding the objects in question by tag, component type or any other means. Then make sure no object is selected when you run it. This might be faster since the selection may not receive a “object removed” event for every building and then doing something with every object and its children … you get the point.

The AssetDatabase has Start/StopAssetEditing methods to batch such edits but this is for assets only. I’m not aware if something like that exists for scene objects.

A last resort could be manually editing the .unity YAML. Provided the buildings are at the bottom of the scene and there’s no other objects in between that you don’t want to be deleted - then you could try finding the first building in the YAML and then just: delete to end (in a backup scene), save file, load the scene in unity. Brute force but it may just work.

For a better workflow I would try to combine these objects when they get dropped in the scene (search for: GameObject change events). If this adds too much delay perhaps the combine can be simplified (eg lossy). You would want to ensure the instantiated building’ changes can’t be applied back to the prefab, ie unpack and with editor scripting you can even reconnect, reset to prefab, unpack again in order to apply prefab changes of buildings to the scene.

1 Like

If these building parts all have one or more components on them, they will run whatever code is in OnDisable and OnDestroy.

You may want to pull out the profiler, put it in edit mode with deep profiling, then delete a single house from an empty scene to check what scripts are doing in such a case.

2 Likes

you should really offline combine these houses either in Unity per script or in 3D software. Even if you can runtime combine them, this will really add to your loading times.