Search Unity

How do I keep a button active on click event with toggle?

Discussion in 'Scripting' started by devprincess, Feb 3, 2013.

  1. devprincess

    devprincess

    Joined:
    Sep 7, 2012
    Posts:
    116
    Hi guys, I'm having trouble with a button, I need to keep active the button until another button is pressed. I have 2 buttons in my scene. I read from forums that the best way is to do it with GUI toggle and I've tried but got no good result.

    So far I've tried this mode:

    Code (csharp):
    1.  
    2.  
    3. private var toggle:boolean;
    4. private var toggleNew :boolean;
    5.  
    6. function OnGUI(){
    7.  
    8. //first button
    9. var toggleNew = GUI.Toggle(Rect(100,100,100,20), false, "Button 1", skin.button);
    10.  
    11.       if(toggleNew != toggle) {
    12.  
    13.               toggle = toggleNew;
    14.  
    15.       }
    16.  
    17. if (GUI.Button (Rect (420,520,200,20), "Button 2")) {
    18.     //second button
    19.    
    20.     }
    21.  
    22. }
    23.  
    could you suggest me any way to solve this? Thanks in advance!
     
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I would just use a boolean and

    add it to the if like if (GUI.Button (Rect (420,520,200,20), "Button 2") toggle)

    then the button only shows when the toggle is true.
     
  3. devprincess

    devprincess

    Joined:
    Sep 7, 2012
    Posts:
    116
    The fact is that i don't want to disappear the button, I just want the following idea:

    When button 1 is pressed, it keeps pressed (and has another appearance) until button 2 is pressed. I still show both buttons on screen.

    Do you know any idea of how to solve this? Thanks anyway!
     
  4. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    if you are using a texture for the button then if(toggle), show texture1, else show texture2.

    That is how I handle that situation because I just made my button invisble using guistyle and then just place textures in their place.
     
    Last edited: Feb 3, 2013
  5. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
  6. devprincess

    devprincess

    Joined:
    Sep 7, 2012
    Posts:
    116
    to show texture of one toggle button what method did you use?
     
  7. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Nothing, just the default "toggle" button skin (you could of course attach your custom skin).

    Toggle button utilizes all of the 8 possible state graphics:

    (other components like GUI.Label, GUI.Button etc. can also use this skin - they are just ignoring the "onX" graphics since not having the "on" state).
     
  8. devprincess

    devprincess

    Joined:
    Sep 7, 2012
    Posts:
    116
    I've tried what you suggested dkozar, but it didn't work, it only works with toggle as you showed before (on the other thread), But i want to simulate the same idea with buttons.

    The idea is the next one:

    I have this 2 buttons, (see pic1)

    Once button1 is clicked, I want button1 to remain red as in pic2

    until button2 is clicked, then button1 should become orange again.

    I will give a try changing the textures as said before by theraider.. but If you have any idea how to solve this I would appreciate it a lot. Thanks anyway!
     
  9. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Just create 8 textures and attach them to mentioned fields of the toggle GUIStyle inside your GUISkin file.
     
  10. devprincess

    devprincess

    Joined:
    Sep 7, 2012
    Posts:
    116
    Is just that I did that, but the red texture only shows on the click event and when I release the button1 it becomes orange again, I want button1 to remain red until another click event happens (button2).
     
  11. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Make 4 orange images and attach them to:

    normal: Rendering settings for when the component is displayed normally.
    hover : Rendering settings for when the mouse is hovering over the control
    active: Rendering settings for when the control is pressed down.
    focused: Rendering settings for when the element has keyboard focus.


    Then make 4 red images and attach them to:

    onNormal: Rendering settings for when the control is turned on.
    onHover: Rendering settings for when the control is turned on and the mouse is hovering it.
    onActive: Rendering settings for when the element is turned on and pressed down.
    onFocused: Rendering settings for when the element has keyboard and is turned on.


    Try if it's working.

    Now you could slightly change the color of each oranges, as well of each reds, to get the subtle mouse-over and mouse-down effect.

    NOTE: the word "on" in onNormal, onHover etc means "when selected".
     
  12. devprincess

    devprincess

    Joined:
    Sep 7, 2012
    Posts:
    116
    Wooooaaah thanks a lot! It worked :)
     
  13. Radivarig

    Radivarig

    Joined:
    May 15, 2013
    Posts:
    121