Undo.RegisterUndo 注册撤销

static function RegisterUndo (objectToUndo : Object, name : string) : void
static function RegisterUndo (objectsToUndo : Object[], name : string) : void



Register the state of one or more objects so the user can later undo back to that state.


Note that only the actual objects passed as parameters are saved. For example, passing a game object will not save all its components or child objects. If you need to save the state of an entire game object, you can use EditorUtility.CollectDeepHierarchy on the game object to get all the objects that it contains and pass all those objects as objects to undo.

注意,只有实际对象参数被保存。例如,不能保存game object的所有的组件或子对象。如果你需要保存整个game object的状态,你可以用EditorUtility.CollectDeepHierarchy

参见: Undo.RegisterSceneUndo.

// Moves the selected game object(s) to (0, 0, 0).
//移动选择的游戏物体到(0, 0, 0)
// Keyboard shortcut: cmd-0 (Mac), ctrl-0 (Windows).
//键盘快捷键cmd-0 (Mac), ctrl-0 (Windows)
// and in case you regret moving them, you can undo the action.

using UnityEditor;
using UnityEngine;

class MoveToOrigin {
	[MenuItem ("Example/Move To Origin %0")]
	static void MenuMoveToOrigin () {
		foreach (Transform t in Selection.transforms) {
			Undo.RegisterUndo(t, "Move " + t.name);
			t.position = Vector3.zero;
	//The menu item will be disabled if no transform is selected.
	[MenuItem ("Example/Move To Origin %0", true)]
	static bool ValidateMoveToOrigin () {
		return Selection.activeTransform != null;
Page last updated: 2011-5-18