Undo.RegisterCompleteObjectUndo

Switch to Manual
public static void RegisterCompleteObjectUndo (Object objectToUndo, string name);

Parameters

objectToUndo需要撤销状态更改的对象。
name撤销操作的名称。

Description

将对象状态存储在撤销堆栈上。

If the undo is performed, any changes made to the object after this function is called will be undone, and the object will be restored to the recorded state.

Transform parent change, AddComponent, and object destruction can not be restored with this function, for that you should use the dedicated functions. See Undo.SetTransformParent, Undo.AddComponent, Undo.DestroyObjectImmediate.

If the object is part of the current Scene (e.g. a game object in the Hierarchy or a component attached to such game object), calling this function will immediately mark the Scene as modified, even if you don't actually change the states of the object afterwards.

using UnityEngine;
using UnityEditor;

public class UndoExamples { [MenuItem("Undo Examples/RegisterCompleteObjectUndo")] static void Example() { GameObject player = new GameObject("Player");

// Store the states of the player object. Undo.RegisterCompleteObjectUndo(player, "Player name change");

player.name = "New Player";

// If you choose "Edit->Undo Player name change" from the main menu now, the name of the object will be restored to "Player". } }

public static void RegisterCompleteObjectUndo (Object[] objectsToUndo, string name);

Parameters

objectsToUndo需要撤销状态更改的一系列对象。
name撤销操作的名称。

Description

这与多次调用第一个重载的效果是一样的,只不过前者只生成一个撤销操作。