• 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
3
Question by MrCranky · Jan 13, 2014 at 02:45 PM · c#undo

What is the correct usage of RegisterCompleteObjectUndo/RegisterFullObjectHierarchyUndo

After upgrading to Unity4, and going through to tweak the usages of RegisterUndo, I note that a couple of extra functions are present in the Undo namespace, with little or no documentation. Specifically RegisterFullObjectHierarchyUndo and RegisterCompleteObjectUndo.

Previously, I'd used RegisterSceneUndo, despite the obvious performance concerns, because it was the best compromise between succinctness in the code and effectiveness. I'm not writing code for a plugin or for sale, this is all just helper code for my own project. RegisterUndo (now RecordObject), as far as I can tell, works on a single Component only. Most of my operations start at a parent GameObject/Component and make changes to children, and cross-linked Components. But unless I put in a RecordObject call for every single Component I touch, this is rather unwieldy. What I'd like to do is put a simple bit of Editor/Undo stuff at the top of the function (with similar SetDirty calls at the button of the function), that records the state of all of the objects I might touch, even if they don't actually get changed. The reason for this is because I have complex iterations / conditionals that mean that I don't know what objects are actually going to be touched by the call, other than that they're all underneath the root object that starts the operation. I'm stuck between having the same conditional logic in two places (and getting out of sync as development continues), or littering a function with Undo.RecordObject calls at the point at which I make a change. That starts to decay as well if I make multiple separate changes to a single component in different places. I have to record the object multiple times at each point of change, just in case the other conditional change didn't get hit.

I got around this before by the brute force method of RegisterSceneUndo, but that is now deprecated. RegisterFullObjectHierarchyUndo and RegisterCompleteObjectUndo tease us with their names, implying that a single call with a root component / object will preserve the state of more than just a single component. But the documentation on them is poor: RegisterCompleteObjectUndo's documentation is empty, but the function is referred to in ClearUndo ('Removes aall[sic] Undo operation for the identifier object registered using Undo.RegisterCompleteObjectUndo from the undo stack.') - and doesn't sound like it does what its name would suggest. I would expect RegisterCompleteObjectUndo to call RecordObject on every component on a GameObject. RegisterFullObjectHierarchyUndo seems even more apt; I'd presume that it takes an Object and calls RecordObject on every component / object on all the children of that object. That would be ideal - 90% of my functions modify only an object and its children.

Are these functions supported, or even properly functional? Does anyone have some solid test cases to show what they do and do not do?

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 huulong · Oct 21, 2016 at 10:52 PM

The Unity documentation has been updated since:

https://docs.unity3d.com/ScriptReference/Undo.RegisterCompleteObjectUndo.html https://docs.unity3d.com/ScriptReference/Undo.RegisterFullObjectHierarchyUndo.html

Undo.RegisterCompleteObjectUndo(Object objectToUndo, string name) will record a copy of the full state of the object that it will keep, unlike Undo.RecordObject(Object objectToUndo, string name) that will only keep a copy of the state until the end of the frame to compute a diff.

By the way, I just got the following error that drove me to find more on this method:

Generating diff of this object for undo because the type tree changed. This happens if you have used Undo.RecordObject when changing the script property. Please use Undo.RegisterCompleteObjectUndo

So RegisterCompleteObjectUndo seems important in complex situations were a diff is not possible.

Undo.RegisterFullObjectHierarchyUndo will indeed copy the state of a game object and all its children and components, but it will react differently with different kind of objects. The documentation gives more details on this.

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 Jamora · Jan 13, 2014 at 02:59 PM

I just use SerializedProperty in my Editor code for full support for undo/redo... The documentation for Editor shows its usage.

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 MrCranky · Jan 13, 2014 at 03:03 PM 0
Share

Fair enough. But I have several hundred components, of varying degrees of complexity, and going back to refit them all as serialised properties isn't likely to happen. Plus I'd still like to know more about these undocumented functions for the cases where SerializedProperty et al. isn't appropriate.

avatar image Jamora · Jan 13, 2014 at 03:11 PM 0
Share

Never having used the methods in question, the best advice I can give you is to get a (free) .net decompiler and see for yourself what Unity does inside those functions. If they're all calls to the native side, you're out of luck.

avatar image HappyMoo · Jan 13, 2014 at 03:19 PM 0
Share

That actually doesn't help here as they just call their C++ code there, so you see nothing.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

How to make object come back 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges