Search Unity

Custom Inspector Undo

Discussion in 'Immediate Mode GUI (IMGUI)' started by delinx32, Jul 28, 2015.

  1. delinx32

    delinx32

    Joined:
    Apr 20, 2012
    Posts:
    417
    I have the following in a very simple custom editor. If I change the value and hit ctrl+z, it does not undo the value change, but instead reverts to the last operation on the undo stack (Selection Change). What do I need to do to this code to make it properly work with ctrl+z?

    If I leave it using the default inspector, ctrl+z works as it should.


    Code (CSharp):
    1.             public override void OnInspectorGUI()
    2.             {
    3.                 _target.Field = EditorGUILayout.FloatField("Field Value:", _target.Field);
    4.     }
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Code (CSharp):
    1. EditorGUI.BeginChangeCheck()
    2.  
    3. float newValue = EditorGUILayout.FloatField("Field Value:", _target.Field);
    4.  
    5. if (EditorGUI.EndChangeCheck()) {
    6.  
    7.     Undo.RecordObject(_target, "Set Field Value");
    8.  
    9.     _target.Field = newValue;
    10. }
     
    delinx32 likes this.
  3. delinx32

    delinx32

    Joined:
    Apr 20, 2012
    Posts:
    417
    Thanks, this worked, but Undo.RecordObject does not work for me. I have to use Undo.RegisterUndo for some reason.