Perform action on save/load in editor

Is there some way to get an editor script callback BEFORE the user saves a scene (i.e. just before the scene is written to disk), and AFTER the user loads a scene (i.e. after the scene’s objects have been loaded in the editor, but before the user makes any changes)?

I want to write a script to validate the scene (and possible modify it) in these two situations, but don’t know how to trigger my script.

Did you ever sort this out? I'm also looking for a solution to this.

2 Answers

2

Performing actions before saving a scene can be done by writing a custom AssetModificationProcessor:

http://docs.unity3d.com/Documentation/ScriptReference/AssetModificationProcessor.OnWillSaveAssets.html

Here’s an example of how we did it:

public class MyAssetModificationProcessor : AssetModificationProcessor
{
    public static string[] OnWillSaveAssets(string[] paths)
    {
        // Get the name of the scene to save.
        string scenePath = string.Empty;
        string sceneName = string.Empty;

        foreach (string path in paths)
        {
            if (path.Contains(".unity"))
            {
                scenePath = Path.GetDirectoryName(path);
                sceneName = Path.GetFileNameWithoutExtension(path);
            }
        }

        if (sceneName.Length == 0)
        {
            return paths;
        }

        // do stuff

        return paths;
    }
}

OnWillSaveAssets will be called every time the user saves the current scene, and you’re unable to switch scenes without saving or abandoning all changes. Thus, there ought to be exactly zero or one scene in the paths array.

If you want Unity not to save any of the modified assets, you can modify the paths array before returning it.

However, changing a scene before it is loaded can’t be done this way and I’m interested in how to do that without introducing a special menu item, too…

Thank you for this example, lots of people appreciate your help. This code doesn't compile. I think it needs some usings. Path for instance isn't found.

Path is part of System.IO: using System.IO; Also, note that AssetModificationProcessor is part of the UnityEditor namespace.

Works! Fine stuff npruehs

there are no examples for this in the Unity docs so thankyou for posting!

Works great, but the problem now is that this doesn't work for Save As..., just when you do Save... It worked for what I had to do, though, just can't stop that user from hitting Save As... as well. I guess they'll hit the Ctrl + S buttons though to save before running....or I'll have to make the processes happen on application start instead...

Not sure, but why not create your own menu item or window that does the save and ask your team to use that. See http://unity3d.com/support/documentation/ScriptReference/EditorApplication.SaveScene.html for ideas.

That's always a possibility, but I'd like to not have to change the normal workflow if possible. Otherwise people can always forget, and there's the constant explanations of "Oh, don't use THAT save button, use THIS one. Why? Well, that's just the one you use...". Also it would break things like the normal double-clicking to load behaviour (for scenes) as well as hotkeys (like CTRL-S to save, which would fire the default save, not my 'special save').