Search Unity

Saving the State of a custom Window

Discussion in 'Scripting' started by SuperMops, Jan 17, 2022.

  1. SuperMops

    SuperMops

    Joined:
    Feb 7, 2018
    Posts:
    28
    Hi,

    I'm trying to learn the basics of custom windows and node editors.
    With the code below I get two windows that I can drag, however when I close and
    reopen the main window or reopen Unity, they are reset and start in the positions
    specified in the script (which of course makes sense).

    How would I approach saving the state of the window, such that it survives reopening
    Unity?

    (I do understand that I could save the current rects of the windows and then retrieve
    them on reloading, but with more complex windows this will be very tedious and I'm
    wondering if there is some buildIn method for custom editors which solves this)

    Code (CSharp):
    1. public class TestEditorWIndow : EditorWindow
    2. {
    3.     Rect window1 = new Rect(10, 10, 100, 100);
    4.     Rect window2 = new Rect(210, 210, 100, 100);
    5.  
    6.     [MenuItem("Window/EditorTestWindow")]
    7.     private static void OpenWindow()
    8.     {
    9.         TestEditorWIndow window = GetWindow<TestEditorWIndow>();
    10.     }
    11.  
    12.     private void OnGUI()
    13.     {
    14.         BeginWindows();
    15.  
    16.         window1 = GUI.Window(1, window1, DrawNodeWindow, "Window 1");
    17.         window2 = GUI.Window(2, window2, DrawNodeWindow, "Window 2");
    18.  
    19.         EndWindows();
    20.  
    21.         TestEditorWIndow window = GetWindow<TestEditorWIndow>();
    22.     }
    23.  
    24.     void DrawNodeWindow(int id)
    25.     {
    26.         GUI.DragWindow();
    27.         GUI.Label(new Rect(10, 20, 100, 20), "My Window");
    28.     }
    29. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I think this guy covers it towards the end of his setup:

     
    SuperMops likes this.
  3. SuperMops

    SuperMops

    Joined:
    Feb 7, 2018
    Posts:
    28
    Thank You. That's an interesting tutorial.
     
    Kurt-Dekker likes this.