Is it possible to ensure that certain game objects don't get saved in the scene or otherwise hook into the default save scene to run custom editor code?

Basically I have some game objects that I'm using in a custom editor. It's a camera that points to a game objects and a render texture so that you can edit the definition for our NPCs and then have a preview of this object in the editor (using the render texture). We really want to keep this pattern because it helps our artists a lot to be able to see the changes they make to the item (equipment, etc). Making a bunch of prefabs for these NPCs isn't exactly an option for our data set and workflow.

Right now we have it set up such that when you close this custom editor the preview camera and object get removed from the scene. The problem is that if you have our custom editor open and then you save the scene you're currently editing, these game objects can begin to stat polluting the scene.

Right now we're semi-working-around-it by making the preview game objects destroy themselves on start, but ideally we could find a way to either 1) hook into the default "save scene" call to run custom code to check for these objects and delete them, 2) somehow otherwise ensure that these game objects don't get saved to the scene, or 3) use some other mechanism for rendering game objects to a texture for our editor other than putting a preview version of them in the current scene.

You can use this ...

gameObject.hideFlags = HideFlags.DontSave;

Which will prevent the objects from being saved, but leaves you on the hook for object management. If you generate objects and set the hideFlags to DontSave then the objects won't get serialized to your scene, but they will hang around in the editor until you delete them yourself.

I'm creating all my game objects under a single root object, and then using this code to clean up the scene in the editor after the game stops playing.

void OnDisable()
{
    if (Application.isEditor)
    {
        if (mRootGameObject != null)
        {
            // Object.Destroy is delayed, and never completes in the editor, so use DestroyImmediate instead.
            UnityEngine.Object.DestroyImmediate(mRootGameObject);
        }
    }
}

See docs for (a little bit) more info.

Note that you can also run custom code at asset save time using a SaveAssetsProcessor. This feature was added in Unity 3.2.