Undo/Redo callback function - which was performed?

Hi,

I have an editor extension which I want to support Undo/Redo operations. I have a callback function which partially works, but I am having an issue:

When I make my change via script, I am registering the change with the Undo interface like so:

Undo.SetCurrentGroupName("MakeMyChange");
int undoGroup = Undo.GetCurrentGroup();
Undo.RecordObjects(myObjects, "MakeMyChange");
// change my objects
Undo.CollapseUndoOperations(undoGroup);

When my objects change, I want to run some code to get them to update their geometry etc, which the Undo operation doesn’t do by itself. So I am using the Undo.undoRedoPerformed callback. I can use Undo.GetCurrentGroupName() to see when my objects were changed (when it equals “MakeMyChange”)… but this only works for Redo operations. What I need then is the name of the Group we have just left, but instead I can only get the name of the group we have just entered (which is why redo works but undo does not).

Of course I can make a variable to keep track of the name of the last group, but in this callback function I don’t think I can determine whether it is an undo or a redo being performed, so I don’t know whether to test the last group name or the current one.

Has anyone encountered this problem before? It’s my first time making an editor extension but it seems this could be quite a common issue.

Or should I be using something like Undo.RegisterFullObjectHeirarchyUndo() in order to get the geometry/lights underneath my objects to automatically change with the undo/redo operations?

Many thanks,
Simon.

I just tried the RegisterFullObjectHeirarchyUndo() function (for each object) in place of RecordObjects and it doesn’t seem to work.

My only solution currently is to ignore the “MakeMyChange” test and to simply run the code after every undo/redo operation, but it is not at all ideal.

I can test the group index by Undo.GetCurrentGroup()… does anyone know if that is always reliably greater than the last value in case of a redo, or smaller in case of an undo?

Edit: it seems that the GetCurrentGroup value is always greater than the last whether its a redo or an undo operation. It seems to increment whenever ANY event happens, even if it’s a mouse click that does nothing, so I can’t use it to keep track of undo/redo operations.

For now I am keeping a record of the last CurrentGroupName, and running my code in the callback when either the CurrentGroupName or my LastGroupName equals “MakeMyChange”. Sure, it means that sometimes the code runs an extra time unnecessarily, but at least it works with both undo and redo now, and if the user spams ctrl-z/y beyond my object’s changes then my code will only run on the first unrelated operation.

Probably this doesn’t solve all of your problems, but I could solve mine (Undo) by naming the group before my group like this.

   Undo.SetCurrentGroupName("Undo.SplitMapHere");
    Undo.IncrementCurrentGroup();

Then this seems to work perfectly:

        static void CheckUndoRedo()
        {
            if (Undo.GetCurrentGroupName() == "Undo.SplitMapHere")
            {
                glSplitSP = null;
                Debug.Log("glSplitSP = null;");
            }
        }