Undo Manual     Reference     Scripting  
Scripting > Editor Classes > Undo
Undo

Lets you register undo operations on specific objects you are about to perform changes on.

Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder. Editor classes are in the UnityEditor namespace so for C# scripts you need to add "using UnityEditor;" at the beginning of the script.

The undo system in Unity works by saving the state of one or more objects before performing a change on those objects. It is similar to taking a backup. To undo the change later, the saved state is restored. Since this reverts the objects to the state they had before performing the change, it will be as if the change never happened.

For instant changes to one or more objects, simply call functions RegisterUndo or RegisterSceneUndo before performing the change that it should be possible to undo.

This is all that needs to be done. Unity will automatically handle Undo and Redo for you after that. PerformUndo and PerformRedo should normally not be used unless you want to create your own user interface to the undo system in addition to the user interface Unity already provides.

Using RegisterUndo is most efficient for actions that change one or more objects. RegisterSceneUndo is slower and requires more memory, but it can be used in all cases, and is required for actions that create or destroy objects.

Certain operations, such as dragging, consist of many small incremental changes. Typically it is not desired to create an undo step for each of these small changes. For example, if the user performs an undo after a dragging operation, it is expected that the object is reverted back to the state it had before the dragging started. The functions SetSnapshotTarget, CreateSnapshot, and RegisterSnapshot are available to handle cases like this.

Class Functions
RegisterUndo

Register the state of one or more objects so the user can later undo back to that state.

RegisterSceneUndo

Register the state of the entire scene so the user can later undo back to that state.

RegisterCreatedObjectUndo

Register the state of a Unity Object so the user can later undo back to that state.

SetSnapshotTarget

Set the objects modified by the GUI or Handles so they can undo properly.

ClearSnapshotTarget

Clear the snapshot target set with SetSnapshotTarget.

CreateSnapshot

Save the current state of all objects set with SetSnapshotTarget to internal snapshot.

RestoreSnapshot

Restore the snapshot state made with CreateSnapshot.

RegisterSnapshot

Register the snapshot state made with CreateSnapshot so the user can later undo back to that state.

PerformUndo

Perform an Undo operation.

PerformRedo

Perform an Redo operation.