objectToUndo | 用于确定需要撤消其状态更改的对象层次结构的对象。 |
name | 撤消操作的名称。 |
将对象层次结构的状态复制到撤消堆栈。
此函数的工作原理类似于 Undo.RegisterCompleteObjectUndo。主要区别在于,此函数不是复制单个对象的状态,而是存储对象层次结构的状态。根据 objectToUndo
的类型,层次结构的确定方式不同
* 如果 objectToUndo
是一个游戏对象,则层次结构将包含 (a) objectToUndo
本身及其子游戏对象;(b) 附加到这些游戏对象的组件。
* 如果 objectToUndo
是附加到现有游戏对象的组件,则层次结构将包含该游戏对象及其所有组件,包括 objectToUndo
。在这种情况下,子游戏对象不包含在内。
* 在所有其他情况下,层次结构将仅包含 objectToUndo
本身。然后它等效于使用相同参数调用 Undo.RegisterCompleteObjectUndo。
如果执行了撤消操作,则在此函数调用后对上述层次结构中对象的任何更改都将被撤消,并且对象将恢复到记录的状态。
无法使用此函数恢复变换父级更改、AddComponent 和对象销毁,为此您应该使用专用函数。请参阅 Undo.SetTransformParent、Undo.AddComponent、Undo.DestroyObjectImmediate。
如果任何涉及的对象是当前场景的一部分(例如,层次结构窗口中的游戏对象或附加到此类游戏对象的组件),则调用此函数将立即将场景标记为已修改,即使您实际上没有随后更改对象的状态。
using UnityEngine; using UnityEditor;
public class UndoExamples { [MenuItem("Undo Examples/RegisterFullObjectHierarchyUndo 1")] static void Example1() { GameObject root = new GameObject("Root"); MeshRenderer rootComponent1 = root.AddComponent<MeshRenderer>(); MeshCollider rootComponent2 = root.AddComponent<MeshCollider>();
GameObject child = new GameObject("Child"); child.transform.parent = root.transform; MeshRenderer childComponent1 = child.AddComponent<MeshRenderer>(); MeshCollider childComponent2 = child.AddComponent<MeshCollider>();
// Store the states of 'root' and its children. Undo.RegisterFullObjectHierarchyUndo(root, "full object hierarchy change");
root.name = "New Root"; child.name = "New Child";
rootComponent1.enabled = false; rootComponent2.enabled = false;
childComponent1.enabled = false; childComponent2.enabled = false;
// If you choose "Edit->Undo full object hierarchy change" from the main menu now, // the states of both game objects and their components will be restored to what they were right before calling Undo.RegisterFullObjectHierarchyUndo. } }
using UnityEngine; using UnityEditor;
public class UndoExamples { [MenuItem("Undo Examples/RegisterFullObjectHierarchyUndo 2")] static void Example2() { GameObject root = new GameObject("Root"); MeshRenderer rootComponent1 = root.AddComponent<MeshRenderer>(); MeshCollider rootComponent2 = root.AddComponent<MeshCollider>();
GameObject child = new GameObject("Child"); child.transform.parent = root.transform; MeshRenderer childComponent1 = child.AddComponent<MeshRenderer>(); MeshCollider childComponent2 = child.AddComponent<MeshCollider>();
// Store the states of 'root' and all of its components. Undo.RegisterFullObjectHierarchyUndo(rootComponent1, "full object hierarchy change");
root.name = "New Root"; child.name = "New Child";
rootComponent1.enabled = false; rootComponent2.enabled = false;
childComponent1.enabled = false; childComponent2.enabled = false;
// If you choose "Edit->Undo full object hierarchy change" from the main menu now, // the states of 'root' and all of its components will be restored to what they were right before calling Undo.RegisterFullObjectHierarchyUndo, // but changes made to 'child' and its components won't be restored. } }
此重载已弃用。请改用 Undo.RegisterFullObjectHierarchyUndo(Object, string)。