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
0
Question by aalmada · Feb 14, 2014 at 07:09 PM · assetdatabaseundo

How to support undo for objects in .asset files

Hi, I'm creating an asset using the following code:

 [MenuItem("Assets/Create/Graph")]
 public static void CreateAssetGraph()
 {
     var asset = ScriptableObject.CreateInstance<Graph>();
     AssetDatabase.CreateAsset(asset, AssetDatabase.GenerateUniqueAssetPath("Assets/NewGraph.asset"));
     AssetDatabase.SetLabels(asset, new string[] { "Graph" });
     AssetDatabase.SaveAssets();
 }

I want to support Undo for all the operations performed on this asset so I'm writing the following before each operation:

 Undo.RecordObject(activeGraph, string.Format("Create {0} Block", blockShape.Name));

The issue is that, after I undo the operation, my object doesn't seem to change.

I added handlers to ValidateCommand and ExecuteCommand events so that when I get a command name "UndoRedoPerformed" I execute:

 EditorUtility.SetDirty(activeGraph);
 AssetDatabase.SaveAssets();

I can see then in the .asset file that the undo was performed and correctly serialized. I tried using AssetDatabase.ImportAsset and AssetDatabase.Refresh and the version in memory never changes. If I close Unity and reopen the project, then the .asset is loaded correctly.

Am I missing something?

Thanks in advance, Antao

Comment
Add comment · Show 2
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 MakeCodeNow · Feb 15, 2014 at 08:26 PM 0
Share

I'm afraid I don't have an answer, but I currently have Undo working for all of my assets, and I'm doing pretty similar things. Here are some ideas:

First off, you want to use SerializiedObject/Serialized property whenever possible. In cases where you can't, then it should be sufficient to record the object, make the change, and then call EditorUtility.SetDirty.

You may need to call Repaint() when the undo happens, depending on where you are drawing the asset properties. The GUI sometimes updates infrequently, which makes it look like your actions had no effect.

You don't need to manually call SaveAssets, unless you are afraid of Unity crashing. $$anonymous$$arking the asset as dirty will (should) let Unity know to save that asset whenever it needs to (like exiting the app).

avatar image aalmada · Feb 16, 2014 at 06:50 PM 0
Share

Thanks for the tips.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aalmada · Feb 16, 2014 at 06:57 PM

I decided to create a simpler project to test these issues. The source is available at https://bitbucket.org/aalmada/assetdemo. I'm still not happy with the editor window not updating when not in focus but I got most of it working. Antao

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 Artifact-Jesse · Sep 08, 2018 at 11:22 PM 0
Share

Any chance you could give a breakdown of what's going on in your scripts in that link? There are a number of similar questions to this one and virtually nothing in Unity's documentation or examples to help out some of us who are less experienced with editor scripting.

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

20 People are following this question.

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

Related Questions

How do I register undo for the moving/renaming of a folder along with the assets contained? 0 Answers

How to allow Undo on project assets created with an editor script? 0 Answers

How to undo AssetDatabase.CreateAsset? 0 Answers

How are unity references serialized? (instanceID related) 1 Answer

ObjectField Get object type(Asset, Scene Object) 2 Answers


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