- Home /
How do you use a Undo from EditorWindow?
/*
My CustomEditorWindow -> "Ctrl+Z(Undo)" is not working!!
Unity Version 4.3.4f1
*/
using UnityEditor;
public class SampleWindow : EditorWindow {
[MenuItem( "Window/Sample Window" )]
static void ShowWindow()
{
EditorWindow.GetWindow( typeof( SampleWindow ), false, "Sample Window" );
}
private SampleClass1 src1;
private void OnEnable()
{
src1 = AssetDatabase.LoadAssetAtPath( "Assets/Resources/SampleObj1.prefab", typeof( SampleClass1 ) ) as SampleClass1;
}
public void OnGUI()
{
var c = src1 as SampleClass1;
string s = EditorGUILayout.TextField( "Text", c.sampleProperty1 );
if( c.sampleProperty1 != s )
{strong text
// Does'nt work.
Undo.RecordObject( c, "SampleClass1/String/SampleProperty1/Change" );
c.sampleProperty1 = s;
}
float value = EditorGUILayout.Slider( "Value", c.slider, 0f, 1f );
if( c.slider != value )
{
// Does'nt work.
Undo.RecordObject( c, "SampleClass1/Float/Slider/Change" );
c.slider = value;
}
}
}
What do you mean by "It's not working"? If you explain it a bit more, maybe someone can help you.
I always record target ins$$anonymous$$d of c, not sure why that would do anything but it's worth a shot maybe.
I've always had issues with Unity's old undo skipping every other step, but the new one has worked as far as I can tell.
Answer by MakeCodeNow · May 21, 2014 at 04:17 AM
Generally what I'd recommend is that you create an inspector window for your type. Then in your other editor Window, you can use Editor.CreateEditor() and call it's OnInspectorGUI from within your Editor code. That gives you both a nice inspector for the asset but still let's you embed the inspector in a more complex GUI (which looks like what you are trying to do).