Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Perform last undo operation and remove it from the stack?

Discussion in 'Scripting' started by TerabyteTim, Jul 1, 2016.

  1. TerabyteTim

    TerabyteTim

    Joined:
    Oct 20, 2011
    Posts:
    115
    So I'm making an editor script, and I'd like to perform the last undo operation, but then remove it from the stack so that there is no available redo operation.

    The docs state the following about Undo.RevertAllInCurrentGroup()
    "Performs the last undo operation but does not record a redo operation. This is useful when you want to simply revert the last performed action."

    This sounds like exactly what I want to do. However when I call this function, nothing happens. It does not undo the last action, and it does not remove anything from the stack.

    Anyone have any ideas why this isn't working? Or any ideas how I could perform an undo and remove it from the stack?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,560
    Well it reverts whatever is the 'currentgroup', you can call 'Undo.GetCurrentGroup' to see what that is.

    If you know the id of the group you want to revert back to you can call the 'RevertAllToGroup' method:
    https://docs.unity3d.com/ScriptReference/Undo.RevertAllDownToGroup.html

    Note, groups are incremented based on events like mouse down, menu execution, etc. If your script are triggering such things, the current group is changing, which might be why 'RevertAllInCurrentGroup' isn't working. Maybe you can record the id of the action you want to revert when it happens...
     
  3. TerabyteTim

    TerabyteTim

    Joined:
    Oct 20, 2011
    Posts:
    115
    Ahh, I see, I thought groups were only incremented when something was added to the undo stack. I think this was the information I needed, I'll test it out and see.