Strip Parts of Scene On Build

Is it possible to process a scene right before it’s queued to be built without modifying project files?

Essentially, I have some scenery and props that is combined using MeshBaker and saved as a GameObject instance in the scene. I prefer this workflow instead of creating tonnes of small prefabs.

The GameObjects used for baking are disabled in the scene, and the result is enabled, so everything works from a gameplay perspective. However, these objects (and any referenced meshes/textures) are still included in the build, bloating its size, when I really only need the baked objects.

There’s hooks like OnPostProcessScene, but it both runs when saving scenes normally, and I don’t know if changing the scene during the build-invocation of PostProcessScene will affect the scene on file.

I understand that this exists. However, it has a scary, unexplained line in the description:

  • You must manually clear the object from memory using DestroyImmediate to avoid memory leaks.

If anyone can confirm that this is a dumb automatic-documentation thing, I’d be happy to use this.

Have a look at Unity - Scripting API: IProcessSceneWithReport. It’s called during the build process and when entering play mode and won’t modify the actual scene asset. We use it to strip GameObjects dependent on the build platform settings.

3 Likes

In addition to what @tomfulghum said, you could also use the EditorOnly Tag on GameObjects you want to exclude from your builds.