Search Unity

First Gui.Toggle disables the following when it should not

Discussion in 'Immediate Mode GUI (IMGUI)' started by KeinZantezuken, Jun 29, 2017.

  1. KeinZantezuken

    KeinZantezuken

    Joined:
    Mar 28, 2017
    Posts:
    53
    Someone please explain me why when I click toggle1 the rest of the toggles (2-3) become hidden from the menu?


    Code (CSharp):
    1. private void OnGUI()
    2. {
    3.     //blah
    4.     toggle1 = GUI.Toggle(new Rect(10f, (float)Screen.height - 35f, 150f, 30f), toggle1, "MODE1");
    5.     if (toggle1)
    6.     {
    7.         action1 = true;
    8.         return;
    9.     }
    10.     action2 = false;
    11.     toggle2 = GUI.Toggle(new Rect(200f, (float)Screen.height - 35f, 150f, 30f), toggle2, "MODE2");
    12.     if (toggle2)
    13.     {
    14.         action2 = true;
    15.         return;
    16.     }
    17.     action2 = false;
    18.     toggle3 = GUI.Toggle(new Rect(400f, (float)Screen.height - 35f, 150f, 30f), toggle3, "MODE3");
    19.     if (toggle3)
    20.     {
    21.         action3 = true;
    22.         return;
    23.     }
    24.     action2 = false;
    25. }
    Removal of the return results in non-working toggles (they work but actionX assigment does not happend)
     
    Last edited: Jun 30, 2017
  2. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    You can't call return while OnGUI beacuse it will stop to draw the next code lines.
    Try something like this:
    Code (csharp):
    1. private void OnGUI()
    2. {
    3.     EditorGUI.BeginChangeCheck();
    4.     toggle1 = GUI.Toggle(new Rect(10f, (float)Screen.height - 35f, 150f, 30f), toggle1, "MODE1");
    5.     if (EditorGUI.EndChangeCheck())
    6.     {
    7.         // Do action1 here
    8.     }
    9.  
    10.     EditorGUI.BeginChangeCheck();
    11.     toggle2 = GUI.Toggle(new Rect(200f, (float)Screen.height - 35f, 150f, 30f), toggle2, "MODE2");
    12.     if (EditorGUI.EndChangeCheck())
    13.     {
    14.         // Do action2 here
    15.     }
    16.  
    17.     EditorGUI.BeginChangeCheck();
    18.     toggle3 = GUI.Toggle(new Rect(400f, (float)Screen.height - 35f, 150f, 30f), toggle3, "MODE3");
    19.     if (EditorGUI.EndChangeCheck())
    20.     {
    21.         // Do action3 here
    22.     }
    23. }
    Documentation here. Enjoy!
     
  3. KeinZantezuken

    KeinZantezuken

    Joined:
    Mar 28, 2017
    Posts:
    53
    Hey thanks, man, but can't use Editor.GUI in that context unfortunately.

    Any other ideas? I've read a few suggestions through search but none of them seems to work, why it is such a hassle to make working toggle that does thing instead of just looking pretty damn
     
    Last edited: Jun 30, 2017
  4. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Whoops, my bad didn't see it wasn't in Editor.

    Try this:
    Code (csharp):
    1.  
    2. private void OnGUI()
    3. {
    4.     bool newToggle1 = GUI.Toggle(new Rect(10f, (float)Screen.height - 35f, 150f, 30f), toggle1, "MODE1");
    5.     if (toggle1 != newToggle1)
    6.     {
    7.         toggle1 = newToggle1;
    8.         if(toggle1)
    9.         {
    10.             // Do action1 here
    11.         }
    12.     }
    13.  
    14.     bool newToggle2 = GUI.Toggle(new Rect(200f, (float)Screen.height - 35f, 150f, 30f), toggle2, "MODE2");
    15.     if (toggle2 != newToggle2)
    16.     {
    17.         toggle2 = newToggle2;
    18.         if(toggle2)
    19.         {
    20.             // Do action2 here
    21.         }
    22.     }
    23.  
    24.     bool newToggle3 = GUI.Toggle(new Rect(400f, (float)Screen.height - 35f, 150f, 30f), toggle3, "MODE3");
    25.     if (toggle3 != newToggle3)
    26.     {
    27.         toggle3 = newToggle3;
    28.         if(toggle3)
    29.         {
    30.             // Do action3 here
    31.         }
    32.     }
    33. }
    34.  
     
    Last edited: Jun 30, 2017
  5. KeinZantezuken

    KeinZantezuken

    Joined:
    Mar 28, 2017
    Posts:
    53
    Nope, unfortunately still does not work. Toggle itself does but actions do not work properly, there neds to be two different depending on toggle state, it is not a button. I tried that already, I spent thousands of year on this dumb issue >_>
     
  6. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    I don't understand what is the problem. What do you want for your actions ?
    If you want than that depends on toggle state, you can do like this:
    Code (csharp):
    1. private void OnGUI()
    2. {
    3.     bool newToggle1 = GUI.Toggle(new Rect(10f, (float)Screen.height - 35f, 150f, 30f), toggle1, "MODE1");
    4.     if (toggle1 != newToggle1)
    5.     {
    6.         toggle1 = newToggle1;
    7.         if(toggle1)
    8.         {
    9.             // MODE1 enabled
    10.         }
    11.         else
    12.         {
    13.             // MODE1 disabled
    14.         }
    15.     }
    16.  
    17.     bool newToggle2 = GUI.Toggle(new Rect(200f, (float)Screen.height - 35f, 150f, 30f), toggle2, "MODE2");
    18.     if (toggle2 != newToggle2)
    19.     {
    20.         toggle2 = newToggle2;
    21.         if(toggle2)
    22.         {
    23.             // MODE2 enabled
    24.         }
    25.         else
    26.         {
    27.             // MODE2 disabled
    28.         }
    29.     }
    30.  
    31.     bool newToggle3 = GUI.Toggle(new Rect(400f, (float)Screen.height - 35f, 150f, 30f), toggle3, "MODE3");
    32.     if (toggle3 != newToggle3)
    33.     {
    34.         toggle3 = newToggle3;
    35.         if(toggle3)
    36.         {
    37.             // MODE3 enabled
    38.         }
    39.         else
    40.         {
    41.             // MODE3 disabled
    42.         }
    43.     }
    44. }
     
    Last edited: Jun 30, 2017