Version: 2021.2
LanguageEnglish
  • C#

Undo

class in UnityEditor

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

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

The Undo system stores delta changes in the undo stack.

Undo operations are automatically combined together based on events, e.g. mouse down events will split undo groups. Grouped undo operations will appear and work as a single undo. To control grouping manually use Undo.IncrementCurrentGroup.

By default, the name shown in the UI will be selected from the actions belonging to the group using a hardcoded ordering of the different kinds of actions. To manually set the name, use Undo.SetCurrentGroupName.

Undo operations store either per property or per object state. This way they scale well with any Scene size.

The most important operations are outlined below:

Modifying a single property:
Undo.RecordObject (myGameObject.transform, "Zero Transform Position");
myGameObject.transform.position = Vector3.zero;

Adding a component:
Undo.AddComponent<RigidBody>(myGameObject);

Creating a new game object:
var go = new GameObject();
Undo.RegisterCreatedObjectUndo (go, "Created go");

Destroying a game object or component:
Undo.DestroyObjectImmediate (myGameObject);

Changing transform parenting:
Undo.SetTransformParent (myGameObject.transform, newTransformParent, "Set new parent");

Static Properties

postprocessModificationsCallback that is triggered whenever a new set of property modifications is created.
undoRedoPerformedCallback that is triggered after an undo or redo was executed.
willFlushUndoRecordInvoked before the Undo system performs a flush.

Static Methods

AddComponentAdds a component to the game object and registers an undo operation for this action.
ClearAllRemoves all undo and redo operations from respectively the undo and redo stacks.
ClearUndoRemoves all Undo operation for the identifier object registered using Undo.RegisterCompleteObjectUndo from the undo stack.
CollapseUndoOperationsCollapses all undo operation up to group index together into one step.
DestroyObjectImmediateDestroys the object and records an undo operation so that it can be recreated.
FlushUndoRecordObjectsEnsure objects recorded using RecordObject or ::ref:RecordObjects are registered as an undoable action. In most cases there is no reason to invoke FlushUndoRecordObjects since it's automatically done right after mouse-up and certain other events that conventionally marks the end of an action.
GetCurrentGroupUnity automatically groups undo operations by the current group index.
GetCurrentGroupNameGet the name that will be shown in the UI for the current undo group.
IncrementCurrentGroupUnity automatically groups undo operations by the current group index.
MoveGameObjectToSceneMove a GameObject from its current Scene to a new Scene. It is required that the GameObject is at the root of its current Scene.
PerformRedoPerform an Redo operation.
PerformUndoPerform an Undo operation.
RecordObjectRecords any changes done on the object after the RecordObject function.
RecordObjectsRecords multiple undoable objects in a single call. This is the same as calling Undo.RecordObject multiple times.
RegisterChildrenOrderUndoStores a copy of the order of the object's children on the undo stack.
RegisterCompleteObjectUndoStores a copy of the object states on the undo stack.
RegisterCreatedObjectUndoRegisters an undo operation to undo the creation of an object.
RegisterFullObjectHierarchyUndoCopy the states of a hierarchy of objects onto the undo stack.
RegisterImporterUndoCopies the state of the importer for the given asset path.
RevertAllDownToGroupPerforms all undo operations up to the group index without storing a redo operation in the process.
RevertAllInCurrentGroupPerforms the last undo operation but does not record a redo operation.
SetCurrentGroupNameSet the name of the current undo group.
SetTransformParentSets the parent of transform to the new parent and records an undo operation.

Delegates

PostprocessModificationsDelegate used for postprocessModifications.
UndoRedoCallbackDelegate used for undoRedoPerformed.
WillFlushUndoRecordDelegate used for willFlushUndoRecord.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961