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

Enable / Disable GUI Components

Discussion in 'Immediate Mode GUI (IMGUI)' started by devel, Jun 9, 2009.

  1. devel

    devel

    Joined:
    Mar 11, 2009
    Posts:
    140
    How can I disable GUI components, i.ex. a textfield, so the user cant modify the text?

    I am wasting a lots of time again looking for simple things :((.. The GUI tutorials or docs are really too basic. So i am happy for any answer, even if the answer is that it isnt possible.

    Thanks.
     
  2. devel

    devel

    Joined:
    Mar 11, 2009
    Posts:
    140
    Many hours later :((..
    Apparently the GUI-System works like a state machine.
    So if I want to disable a button / textfield
    I simply do:

    1.) GUI.enabled = false;
    2.) GUI.TextField(...);
    3.) GUI.enabled = true;

    It would have been great to read that somewhere.
    Is this the right way to do it, or is there a better way?
     
  3. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    That's the way to do it. I'm not sure whether the topic of enabling/disabling is explicitly covered, but in case you haven't read it check out the GUI Scripting Guide.
     
  4. iceherosubzero

    iceherosubzero

    Joined:
    Jul 10, 2009
    Posts:
    36
    In a GUI Screen where I have many Buttons. Is it possible to disabled only a few of them. can I access them with any button NAME type.
    For Example:
    BUY CAR
    SELL CAR
    CONTINUE
    I want to disabled SELL CAR and CONTINUE unless the user has bought any car or any car is present in the database profile of the user. when he selects any car then i want to enable CONTINUE.
    Thanks.
     
  5. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    You can't access GUI objects by name for this purpose so you'll have to adapt the same technique as above. One way to go might be for you to have variables (buyEnabled, sellEnabled, continueEnabled) and then you toggle those values as needed and then...

    Code (csharp):
    1. GUI.enabled = buyEnabled;
    2. // Draw your buy button here
    3.  
    4. GUI.enabled = sellEnabled;
    5. // Draw your sell button here
    6.  
    7. GUI.enabled = continueEnabled;
    8. // Draw your continue button here
    9.  
    10. GUI.enabled = true; // turn the GUI back on to be sure

    Something like that should work well enough. :)
     
  6. DIProgan

    DIProgan

    Joined:
    Dec 21, 2009
    Posts:
    16
    Where exactly are you supposed to type this GUI.enabled = false; line. My GUI elements refuse to go away from it (via a button for example). Need more about this in the GUI tutorials big time.
     
  7. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    You precede each particular button or other control with your state machine driving whether it's enabled or not. Higgy shows this in his example.

    From the Unity manual: the controls won't 'go away.' If that's what you desire, you need to specifically drive that with a state machine, and not simply whether the control can be clicked on or not.

    Look a couple threads down from you, for someone who had a similar question.

    http://forum.unity3d.com/viewtopic.php?t=40802
     
  8. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Well for starters, have you read the docs about GUI.enabled so you know what it does? I ask that as you seem to want it to cause things to "go away" and that is specifically not what it does. From the docs:

    Note the bit where it notes that they'll draw, but semi-transparently. So GUI.enabled won't make anything go away. If you want that to happen then you need to track variables about whether to show something or not and then do something like this:

    Code (csharp):
    1. var ShowButton = true;
    2.  
    3. function OnGUI () {
    4.  
    5.   if (ShowButton) {
    6.     // draw your button here
    7.   }
    8.  
    9. }

    Does that help?
     
  9. DIProgan

    DIProgan

    Joined:
    Dec 21, 2009
    Posts:
    16
    Thanks it did.
     
  10. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Awesome, rock on!
     
  11. Marisa

    Marisa

    Joined:
    Jan 20, 2012
    Posts:
    3
    That was very helpful, thanks for answering to your own thread :p
     
  12. soul-of-creativity

    soul-of-creativity

    Joined:
    Jun 10, 2016
    Posts:
    2
    how can I apply this script for all my GUI components ???
     
  13. bodiamykhats

    bodiamykhats

    Joined:
    Jul 10, 2022
    Posts:
    1
    Do it with EditorGUI.DisabledScope and "using" keyword.

    Example:
    Code (CSharp):
    1. var selected = Selection.gameObjects;
    2. bool noObjectSelected = selected.Length == 0;
    3. using (new EditorGUI.DisabledScope(noObjectSelected))
    4. {
    5.     if (GUILayout.Button("Snap to grid")) Snap(selected);
    6. }
     
    Fressbrett likes this.