Legacy Documentation: Version 5.2
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Undo.RecordObject

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

Parameters

Description

Records any changes done on the object after the RecordObject function.

Almost all property changes can be recorded with this function. The transform parent, AddComponent, object destruction can not be recorded with this function, for that you should use the dedicated functions.

Internally this will create a temporary copy of the objects state and at the end of the frame Unity will diff the state and thus detect what exactly has changed. The changed properties are then recorded on the undo stack. If nothing has actually changed (Binary exact comparison is used for all properties), no undo operation will be stored on the stack.

using UnityEngine;
using UnityEditor;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Start() { Undo.RecordObject (Selection.activeTransform, "Edit Transform"); Selection.activeTransform.position = Vector3(0, 0, 0); } }
// Editor Script Side
// Create a position Handle and make the target always look at the position handle.
// This is an editor Script, this should go inside the Editor Folder.

[CustomEditor (typeof(LookAtPoint))] class SnapshotTargetEx : Editor {

void OnSceneGUI () { LookAtPoint lookAtScript; EditorGUI.BeginChangeCheck (); Vector3 pos = Handles.PositionHandle(target.lookAtPoint, Quaternion.identity); if (EditorGUI.EndChangeCheck ()) { Undo.RecordObject (target, "Move point"); target.lookAtPoint = pos;

// Make sure to call SetDirty otherwise the inspector will not know that the script properties were modified EditorUtility.SetDirty (target); } } }