x


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.

more ▼

asked Oct 12 '11 at 03:34 PM

AntonStruyk gravatar image

AntonStruyk
76 9 11 14

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

Jun 15 '12 at 02:38 PM Marcs
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Oct 13 '11 at 11:42 AM

Graham Dunnett gravatar image

Graham Dunnett ♦♦
11.8k 11 20 64

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').

Oct 13 '11 at 01:17 PM AntonStruyk
(comments are locked)
10|3000 characters needed characters left

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...

more ▼

answered Jul 12 '12 at 09:36 AM

npruehs gravatar image

npruehs
1 2 2 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3331
x1674
x437
x436

asked: Oct 12 '11 at 03:34 PM

Seen: 1535 times

Last Updated: Jul 12 '12 at 09:36 AM