Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. 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,123
    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.