Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by AntonStruyk · Oct 12, 2011 at 03:34 PM · editorsaveload

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Marcs · Jun 15, 2012 at 02:38 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
15

Answer by npruehs · Jul 12, 2012 at 09:36 AM

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image psantoki · May 05, 2014 at 11:39 PM 1
Share

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.

avatar image npruehs · May 06, 2014 at 08:38 AM 0
Share

Path is part of System.IO:

 using System.IO;

Also, note that Asset$$anonymous$$odificationProcessor is part of the UnityEditor namespace.

avatar image psantoki · May 06, 2014 at 09:48 PM 0
Share

Works! Fine stuff npruehs

avatar image robot-ink · Jun 06, 2014 at 04:04 AM 0
Share

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

avatar image astracat111 · Apr 07, 2017 at 03:52 AM 0
Share

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 ins$$anonymous$$d...

avatar image
-2

Answer by Graham-Dunnett · Oct 13, 2011 at 11:42 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AntonStruyk · Oct 13, 2011 at 01:17 PM 4
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to script a Save/Load option on IPhone? 1 Answer

Using EditorUtility.OpenFilePanel outside editor? 1 Answer

Save scene event when the user save the scene in the editor 1 Answer

Save variable value. 1 Answer

Saving data 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges