Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

How can I use Undo for an EditorWindow?

Discussion in 'Immediate Mode GUI (IMGUI)' started by JoePatrick, May 11, 2019.

  1. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    83
    So I have made a few custom inspectors before and used the Undo system but how do you use it for an EditorWindow when there are no objects to pass to Undo.RecordObject(), I just want to be able to undo changes to local variables.
    Here is what I have
    Code (CSharp):
    1. namespace RapidIconUIC
    2. {
    3.     public class RapidIconWindow : EditorWindow
    4.     {
    5.         static RapidIconWindow window;
    6.  
    7.         public AssetList assetList;
    8.         public DraggableSeparator leftSeparator, rightSeparator;
    9.         public AssetGrid assetGrid;
    10.         public IconEditor iconEditor;
    11.  
    12.         [MenuItem("Tools/RapidIcon")]
    13.         static void Init()
    14.         {
    15.             window = (RapidIconWindow)GetWindow(typeof(RapidIconWindow), false, "RapidIcon");
    16.             window.minSize = new Vector2(870, 600);
    17.             window.Show();
    18.         }
    19.  
    20.         private void OnEnable()
    21.         {
    22.             //A load of init stuff, including this:
    23.             iconEditor = new IconEditor(assetGrid, window);
    24.             iconEditor.LoadData();
    25.         }
    26.  
    27.         private void OnDisable()
    28.         {
    29.             //Saves some variables to EditorPrefs
    30.             iconEditor.SaveData();
    31.         }
    32.  
    33.         void OnGUI()
    34.         {
    35.             if (!window)
    36.                 Init();
    37.  
    38.             //A load of stuff including:
    39.             iconEditor.Draw(window.position.width - rightSeparator.value, window);
    40.         }
    41.     }
    42. }
    Then in the IconEditor class (doesn't derive from anything), I have a bunch of variables that are changed with GUI controls e.g.
    Code (CSharp):
    1. void DrawTransformControls()
    2.         {
    3.             EditorGUI.BeginChangeCheck();
    4.             Vector3 camPos = EditorGUILayout.Vector3Field("Camera Position", currentIcon.cameraPosition);
    5.             //And some others
    6.             if (EditorGUI.EndChangeCheck())
    7.             {
    8.                 Undo.RecordObject(this, "IconEditor Undo"); //Can't do this as IconEditor is not an Object
    9.                 currentIcon.cameraPosition = camPos; //bad example as I'm changing another object here, but sometimes I do just change a local variable to the IconEditor class
    10.                 //currentIcon also cannot be converted to an Object either anyway
    11.                 UpdateIcon(currentIcon);
    12.             }
    13.         }
    So how can I add undo functionality in this script, and others like it

    Thanks :)
     
    Last edited: May 11, 2019
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,850
    You can mark IconEditor as Serializable, and then pass the RapidIconWindow to the IconEditor to record that.

    When doing Undo/Serialization in an Editor Window, it's the editor window itself you're serializing, as it's a ScriptableObject that Unity does some magic to. Exactly the rules for what serializes and why is undocumented and a bit esoteric, but for the most part it works as usual.
     
  3. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    83
    Thanks, I will give that a go :)
     
  4. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    83
    That worked, thanks a lot :D