- Home /
How to deal with object deletion Undo states and variables referencing the objects involved?
This is an editor scripting question. Say I have a List (edit - Built in Array) of GameObjects. I have the user of my editor extension delete some of them from the scene. I register that deletion with the Undo system by using: Undo.DestroyImmediate(MyObject). At the time of deletion I also remove the objects from my List (edit - Built in Array). When the user then presses 'Undo', the objects are re-created by Unity's built in processes. How can I restore them to my list(edit - Built in Array)?
BTW, the objects in question are created by my editor extension and added to the List (edit- Built in Array). Their creation is registered with the Undo system via Undo.RegisterCreatedObjectUndo(myObject,"$$anonymous$$yObjectCreated"). I would similarly be interested if their addition to the list (edit - Built in Array) could also be registered. It's basically the same question, but in reverse. On the one hand, If I could catch the undo event for objects being created (ie. when the user undoes object creation), I could then verify the List(edit - Built in Array) by checking what is in the scene, but what I'm really after is a more direct way of knowing what's being undone/redone (un-created/re-created) so I can get my List(edit - Built in Array) updated accordingly in both cases. This would come in handy in so many situations.
I edited my question to specify that I'm actually using a Built in Array. I realize that Lists are not serialized by Unity, making them incapable of registering with the Undo system. I didn't want anyone to think that this was the crux of my issue. I also want to point out that the Array in question lives in a script on a hidden object that is always in the scene, not in the editor class. So the only thing I'm looking for is how I can re-populate my array with objects that were in it before being deleted and then then restored with Undo.
I was thinking that, as an alternative to using the Undo system to manage my array directly, I could potentially catch the undo event and then compare what's in the scene at the time to a separate array of all objects that have ever been created by my script. Based on that comparison, I can then update my main array of existing objects belonging to my editor extension. This way I would always be doing scene checks to manage my array ins$$anonymous$$d of ever registering the array with the Undo system. The downside is that the array of all stuff ever created by my script would likely get too huge to use efficiently. So this is not an ideal solution.
I just wanted to jump in and say that I have the same issues. It's not related to List, most likely. I've serialized it a lot before (I'm using it in my plugin Shader Forge), without any issues.
It's a node based shader editor, and my issues are related to undoing the deletion of nodes. Each node is also in a List inside a node manager.
The strange part is that I can undo adding nodes, and then redo, which is essentially the same as undoing deletion of a node. (Restoring a lost object and re-adding it to a list without breaking references)
I did try and get this to work in an isolated case, and it actually worked, but I can't get it to work in Shader Forge just yet.
I'll let you know if I can get things to work!
This is the exact problem I had a while back. See here. I managed to reach a 50/50 chance of getting a successful undo.
Answer by Nims · Apr 24, 2014 at 05:31 AM
I think this is relevant to this question:
http://feedback.unity3d.com/suggestions/extend-undo-to-support-actions-command-pattern
Here are just a few thoughts @SebastianPatric:
Implement your own undo/redo system in your editor. So you will need to add Undo/Redo buttons to your editor window.
Make your base items of the list/array seriliazable.
Define a custom class - to record the actions done in your editor window - with some actions like create,delete etc (Enums?), with each action taking in the relevant info (A deep copy of the instance of your list item - this could be a serialized version of the instance?).
Define a stack of of type action in the custom class - This could be a static variable of the custom class. Whenever an action is taken in your editor, create the appropriate action object and push it to the stack.
Add Undo/Redo functions linked to the buttons in the editor with their implementation for the Undo/Redo the action.
Answer by dahodd · Feb 06, 2015 at 10:04 AM
You most likely do an undo on a database or file. You have to repopulate the array from data source after undo.
Your answer
Follow this Question
Related Questions
Undo.RecordObject isn't working. 7 Answers
How to properly handle Undo events in custom inspector? 0 Answers
how to stop reorderable list from passing Event value to the new element 1 Answer
Create a custom window and set his position on second screen 1 Answer
How to undo removing components in an editor extension? 0 Answers