Exclude certain child objects when saving prefab?

I have a prefab object in the scene. This object has a custom inspector and various editor features to provide visualization to the artist. Part of this visualization creates child objects that are solely for vis purposes and should NOT be saved as part of the prefab.

Right now an artist has to remember to turn these off before saving the prefab but eventually someone will forget and make a right mess of things.

So I tried hooking into the ISerializationCallbackReceiver methods to do things before and after serialization. I thought, I could destroy the child vis objects just before serialization and then recreate them after.

Unfortunately Unity won’t let me do this. Apparently because serialization runs on a separate thread, I can’t actually manipulate my object in ways such as finding/destroying/creating sub objects.

Has anyone else been in a similar situation and found ways to handle it?

All the time… just make a DestroyNotInEditor.cs that conditionally destroys the object in Awake(), something like:

 void Awake()
 {
   #if !UNITY_EDITOR
     Destroy( gameObject);
   #endif
 }

Hang that on any object you want to go away at runtime. You can also use Application.IsEditor or whatever that property is called.

Yeah I am basically doing this now (slightly differently but yeah) but I was hoping to not have the runtime overhead of having to “clean up” potentially hundreds of objects by just making sure they don’t get saved to the prefab in the first place.

it’s been 3 years but, for others.

use gameobject.hideFlage = HideFlags.DontSave