Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

GUI Button Active State

Discussion in 'Immediate Mode GUI (IMGUI)' started by beco13, Jun 27, 2011.

  1. beco13

    beco13

    Joined:
    May 31, 2011
    Posts:
    30
    Hi All,

    I'm having this problem,

    I want a button remains active till other button is pressed....How do I control the Button State?


    anyone help me please.... Thx

    =======
    Enrico
    =======
     
  2. slide

    slide

    Joined:
    Feb 2, 2011
    Posts:
    72
  3. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    You can't really control a Button state because once u let go, the button will return true. However you can like what slide said, use a toollbar/selection grid but that is when you need multiple buttons and clicking one of them changes to another one.

    So is like only one is down state at all time. And if thats not what you want. You can however use a toggle. How? Use a toggle and use a button skin. There when you clicked on the toggle, it will remain on the down state until u click it again.

    Code (csharp):
    1.  
    2. var skin : GUISkin;
    3. var toggle : boolean;
    4. function OnGUI()
    5. {
    6.      toggle = GUI.Toggle(Rect(0, 0, 100, 100), toggle, "Toggle Button", skin.button)
    7. }
    8.  
     
    altschuler likes this.
  4. beco13

    beco13

    Joined:
    May 31, 2011
    Posts:
    30
    @zine 92: your advice is really helpful.. :)
    the next thing is , how do I set every unique button to have its own style
    I tried this

    but the result comes up with every button changes its stye according to toolbarInt......

    how do I set every style in every single button in toolbar buttons?
     
  5. nedelkosm

    nedelkosm

    Joined:
    Dec 6, 2013
    Posts:
    10
    Greetings,

    I know this is an old post, but for people looking for a solution i would suggest
    creating some state variables (probably bool) and draw different styles based on that.

    For example:
    Code (CSharp):
    1. if(buttonActive){
    2.       btn = GUI.Button(new Rect(..), texture1, style1);
    3. } else {
    4.       btn = GUI.Button(new Rect(..), texture2, style2);
    5. }
    6. if(btn){
    7.       buttonActive = true;
    8.       // if you have many buttons to handle you can edit their state here
    9.       // An alternative is to keep an index of the selected button (if one) and draw based on that.
    10.       // Eg. for(int i=0; i<totalButtons; i++) if(selectedBtn == i){ GUI.Button(1); } else { GUI.Button(2); }
    11. }
     
  6. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    you Dont need to code in two different textures for a GUI Toggle , use a GUIStyle and no nned to declare a texture , in he inspector on the GUIStyle place the inactive texture on the Normal box and the Active texture in the OnNormal Box