让您可以针对要执行更改的特定对象注册撤销操作。
撤销系统可以在撤销堆栈中存储增量更改。
系统会根据事件自动将多个撤销操作组合在一起,例如,根据鼠标按下事件拆分撤销组。分组后的撤销操作将作为单个撤销操作出现和使用。要手动控制分组方法,请使用 Undo.IncrementCurrentGroup。
默认情况下,系统将按照不同类型操作的硬编码顺序,从属于该组的各项操作中选择 UI 中显示的名称。要手动设置名称,请使用 Undo.SetCurrentGroupName。
撤销操作会存储每个属性或每个对象的状态。这样,如论场景的大小如何,它们都可以正确缩放。
最重要的几项操作如下所述:
修改单个属性:\
Undo.RecordObject (myGameObject.transform, "Zero Transform Position");
\
myGameObject.transform.position = Vector3.zero;
添加组件:\
Undo.AddComponent<RigidBody>(myGameObject);
创建新的游戏对象:\
var go = new GameObject();
\
Undo.RegisterCreatedObjectUndo (go, "Created go");
销毁游戏对象或组件:\
Undo.DestroyObjectImmediate (myGameObject);
更改变换组件的父子化:\
Undo.SetTransformParent (myGameObject.transform, newTransformParent, "Set new parent");
postprocessModifications | 每当创建一组新的属性修改时,将触发此回调。 |
undoRedoPerformed | 执行撤销或重做后触发的回调。 |
willFlushUndoRecord | 在撤销系统执行刷新之前调用。 |
AddComponent | 向游戏对象添加组件并针对这一操作注册撤销操作。 |
ClearAll | 分别从撤销堆栈和重做堆栈中删除所有撤销和重做操作。 |
ClearUndo | 从撤销堆栈中删除使用 Undo.RegisterCompleteObjectUndo 注册的标识符对象的所有撤销操作。 |
CollapseUndoOperations | 将组索引之下的所有撤销操作折叠到一个步骤中。 |
DestroyObjectImmediate | 销毁对象并记录撤销操作,以便能够重新创建该对象。 |
FlushUndoRecordObjects | 确保使用 RecordObject 或 ::ref:RecordObjects 记录的对象被注册为可撤销的操作。在大多数情况下,不需要调用 FlushUndoRecordObjects,因为在释放按下的鼠标和通常标志着操作结束的某些其他事件之后,这一操作会自动完成。 |
GetCurrentGroup | Unity 根据当前的组索引自动对撤销操作进行分组。 |
GetCurrentGroupName | 获取将在 UI 中显示的当前撤销组的名称。 |
IncrementCurrentGroup | Unity 根据当前的组索引自动对撤销操作进行分组。 |
MoveGameObjectToScene | 将游戏对象从当前场景移动到新场景中。 要求游戏对象必须位于当前场景的根目录下。 |
PerformRedo | 执行重做操作。 |
PerformUndo | 执行撤销操作。 |
RecordObject | 记录执行 RecordObject 函数之后对对象所做的任何更改。 |
RecordObjects | 将多个可撤销对象记录到单个调用中。这与多次调用 Undo.RecordObject 的效果相同。 |
RegisterCompleteObjectUndo | 将对象状态存储在撤销堆栈上。 |
RegisterCreatedObjectUndo | 针对新创建的对象注册撤销操作。 |
RegisterFullObjectHierarchyUndo | 将某个层级视图中所有对象的状态复制到撤销堆栈上。 |
RevertAllDownToGroup | 执行组索引之下的所有撤销操作,但在过程中不会存储重做操作。 |
RevertAllInCurrentGroup | 执行最后一次撤销操作,但不记录重做操作。 |
SetCurrentGroupName | 设置当前撤销组的名称。 |
SetTransformParent | 将变换组件的父项设置为新的父项,并记录撤销操作。 |
PostprocessModifications | postprocessModifications 的委托。 |
UndoRedoCallback | undoRedoPerformed 的委托。 |
WillFlushUndoRecord | willFlushUndoRecord 的委托。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.