Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need Help: GUI Toggle Deselects When Editor Window Closed

Discussion in 'Immediate Mode GUI (IMGUI)' started by Controser, Apr 8, 2020.

  1. Controser

    Controser

    Joined:
    Feb 3, 2018
    Posts:
    9
    I had a quick question regarding Unity Editor Windows and wondered if anyone could help me or point me to someone who could. I made a GUI toggle but when it is selected and you close the window, the next time you open the window it is deselected. Is there a way to make it stay on if clicked?

    Code (CSharp):
    1. public bool DarkMode { get; private set; }
    2.  
    3. DarkMode = EditorGUILayout.ToggleLeft("Enable Dark Mode", DarkMode);
    4.  
    5.             if (DarkMode)
    6.                 GUI.backgroundColor = new Color32(60, 60, 60, 255);
    7.             else
    8.                 GUI.backgroundColor = Color.white;
    I wrote the code to change my text blocks to a dark grey if the toggle is selected
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,297
    You can use EditorPrefs to make the selected value persist even if the window is closed.

    Code (CSharp):
    1. public bool DarkMode
    2. {
    3.     get
    4.     {
    5.         return EditorPrefs.GetBool("DarkMode");
    6.     }
    7.    
    8.     set
    9.     {
    10.         return EditorPrefs.SetBool("DarkMode", value);
    11.     }
    12. }
     
  3. Controser

    Controser

    Joined:
    Feb 3, 2018
    Posts:
    9
    Thanks a lot! I wasn't even aware of EditorPrefs but it worked like a charm!
     
    SisusCo likes this.