Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Pecek · Apr 15, 2018 at 06:40 AM · editor-scriptingundo

How to use the Undo class to record editor script property changes?

The issue is fairly obvious

  EditorGUI.BeginChangeCheck();
  myFloat= EditorGUILayout.Slider("Slider", myFloat, -3, 3);
  if (EditorGUI.EndChangeCheck())
  {
      Undo.RecordObject(this, "float");
      EditorUtility.SetDirty(this);
  }

Since we already changed the value myFloat it doesn't make much sense to call Undo.RecordObject() - but we shouldn't call it before that either because the EditorGUILayout.Slider() is changing it every frame basically regardless of user input). Is there a built in way to support undo/redo in editor scripts or do I have to manually create another variable for every variable I want to modify?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Pecek · Apr 15, 2018 at 11:05 AM

The EditorUndoManager from the wiki worked well after some slight modifications(it's kind of expected as it's 7 years old at this point). If anyone is interested here is the link

http://wiki.unity3d.com/index.php?title=EditorUndoManager

I removed the obsolete stuff and changed them to Undo.RegisterCompleteObjectUndo(). Also the properties have to be either public or marked as [Serialized](it sounds obvious as I type it but it took me a good 20 minutes to figure out why does it work with some of my changes while it doesn't with others).

Comment
Add comment · Show 3 · 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 IgorAherne · Oct 08, 2018 at 09:25 AM 0
Share

Thanks! So the trick indeed was adding [SerializeField] and ensuring the types are using [Serialized] attribute, inside the editor script. I use them all the time for monobehaviors, but writing these attributes directly inside the editor script is kind of unusual to me :D

avatar image HarryBGamer2570 · Mar 19 at 04:13 PM 0
Share

You can also use "[SerializeField] [HideInInspector]" if you still want to hide it and to be private

avatar image Bunny83 HarryBGamer2570 · Mar 19 at 04:45 PM 0
Share

Right, I mentioned this combination at the end of my answer over here. The table I created is ordered by precedence from top to bottom. So if multiple things apply, things at the top would overwrite things further down.

avatar image
1

Answer by SmushyTaco · May 08, 2020 at 07:02 AM

If you don't want to rely on 3rd party code you could do the following:

     EditorGUI.BeginChangeCheck();
       var myFloatForUndoCheck = myFloat;
       myFloatForUndoCheck = EditorGUILayout.Slider("Slider", myFloatForUndoCheck, -3, 3);
       if (EditorGUI.EndChangeCheck()) {
           Undo.RecordObject(this, "My Float");
           myFloat = myFloatForUndoCheck;
       }
Comment
Add comment · 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

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

88 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is play mode reverting my scriptableobject to a previous serialized state? 2 Answers

Undo.RecordObject is too slow on large arrays, alternatives? 1 Answer

Undo SetSiblingIndex for root objects? 1 Answer

How to undo removing components in an editor extension? 0 Answers

Is there a way to register undo for all ColorField changes, or name the individual undos? 0 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