Version: 2020.1

Undo.RegisterFullObjectHierarchyUndo

切换到手册
public static void RegisterFullObjectHierarchyUndo (Object objectToUndo, string name);

参数

objectToUndo 在某个层级视图中所有对象的状态更改需要撤销时,用于确定这些对象的对象。
name 撤销操作的名称。

描述

将某个层级视图中所有对象的状态复制到撤销堆栈上。

执行这一函数的结果与 Undo.RegisterCompleteObjectUndo 类似。关键的区别在于,执行这一函数不是复制单个对象的状态,而是存储某个层级视图中所有对象的状态。具体的层级视图因 objectToUndo 的类型而异:

* 如果 objectToUndo 为游戏对象,则层级视图中将包含:(a) objectToUndo 本身及其子游戏对象;(b) 附加到这些游戏对象上的组件。

* 如果 objectToUndo 为附加到某个现有游戏对象上的组件,则层级视图中将包含该游戏对象及其所有组件,包括 /objectToUndo/。在这种情况中,未涉及子游戏对象。

* 在所有其他情况下,层级视图中仅包含 objectToUndo 本身。这相当于使用相同的参数调用 Undo.RegisterCompleteObjectUndo

如果执行了撤销操作,则在调用这一函数后对上述层级视图中的对象所做的任何更改都将被撤销,并且对象将恢复到记录的状态。

无法使用这一函数恢复变换组件父项的更改、AddComponent 和对象销毁,应该使用专用函数来恢复。请参阅 Undo.SetTransformParentUndo.AddComponentUndo.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. } }

Obsolete public static void RegisterFullObjectHierarchyUndo (Object objectToUndo);

描述

此重载已弃用。请使用 Undo.RegisterFullObjectHierarchyUndo(Object, string)。