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
2
Question by Alex-Chouls · Feb 01, 2011 at 04:52 PM · undosnapshot

How to use the Undo snapshot system?

I'm trying to understand how to use Undo.SetSnapshotTarget etc.

The docs are a little sparse and there don't seem to be any examples out there...

Does anyone have any hints?

I've tried setting the SnapshotTarget, then CreateSnaphot before a change, and RegisterSnapshot after the change, but the change never shows up in the Undo menu.

Unfortunately, I'm adding it to a fairly complex GUI so I can't post example code. I'll try a simpler test case if I can't get it working... but was hoping for some pointers...

Thanks!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by yoyo · Jun 26, 2012 at 02:53 PM

Undo.RegisterUndo basically says "this item is about to change, so please remember its state so I can undo it later." It should only be called when you know the target object is about to be changed.

In contrast, the snapshot system lets you handle the case where you don't know if the object will be changed or not -- you can save the state of the object and decide later whether or not to register that state for an undo operation.

Note that the simple:

 Undo.RegisterUndo(target, "Modify");

Is equivalent to:

 Undo.SetSnapshotTarget(target, "Modify");
 Undo.CreateSnapshot();
 Undo.RegisterSnapshot();

You can use the snapshot system like this in your custom editor:

 public override void OnInspectorGUI()
 {
     // Create the snapshot before you start editing.
     Undo.SetSnapshotTarget(target, "Modify");
     Undo.CreateSnapshot();

     // Do whatever you want to edit your object.
     DoMyFunkyCustomEdits();

     // If something changed then register the snapshot for undo.
     // You can either use Unity's GUI.changed or keep track of your
     // own flag indicating that your code modified something.
     if (GUI.changed)
     {
         EditorUtility.SetDirty(target);
         Undo.RegisterSnapshot();
     }
 }

Note that Unity is clever enough to group related Undo operations -- a sequence of operations on the same object with the same name (e.g. "Modify" above) will turn into a single undo-able operation.

Note that this answer is relevant to the question, but the solution seems more complex than necessary. What I've documented here is working for me, I hope it helps others.

Alex, it sounds like this is almost exactly what you're doing, and that AndyP's suggestion of calling EditorUtility.SetDirty may be all you're missing. (Disclaimer: in my custom editor I also call SerializedObject.SetIsDifferentCacheDirty, though I'm not certain it's needed.)

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

Answer by DaveA · Feb 01, 2011 at 05:37 PM

Use Register before the change: http://unity3d.com/support/documentation/ScriptReference/Undo.RegisterSceneUndo.html

But it sounds like one or the other: user Register to capture the entire state of the editor, use Snapshot on particular objects.

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

Answer by AndyP · Jun 26, 2012 at 05:07 AM

I'm assuming you are trying to make undo work with handles that update something continuously as the user moves them. You didn't make it absolutely clear in your question, but I'll answer with what I know anyway.

Have you tried calling EditorUtility.SetDirty? I found adding a call to this magically makes snapshots work without anything other than SetSnapshotTarget. Here's an example:

Undo.SetSnapshotTarget(target, "move point");

for (i = 0; i < target.points.Length; ++i) { Vector3 newPoint = Handles.FreeMoveHandle( target.points[i], Quaternion.identity, handleSize, pointSnap, Handles.DotCap); }

if (GUI.changed) EditorUtility.SetDirty(target);

Disclaimer: What I'm doing is not how I read it was supposed to be done, but it works for me. However, I currently have a weird problem with the function that contains this code and I'm not sure whether or not it is the cause.

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 Berenger · Jun 26, 2012 at 05:08 AM 0
Share

Your answer doesn't show up right away until you have 15 karma AndyP, just have to be patient until then.

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

2 People are following this question.

avatar image avatar image

Related Questions

How to undo AssetDatabase.CreateAsset? 0 Answers

How to clear Undo-history for the target of a serializedObject? 0 Answers

store a reference to a float in a variable? (similar to what the "ref" keyword does) 2 Answers

Custom Inspector Undo Functionality when directly changing SerializedProperties 1 Answer

How do you emulate the undo system with custom inspectors? 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