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

Toolbar with no selection?

Discussion in 'Immediate Mode GUI (IMGUI)' started by bigkahuna, Jan 27, 2009.

  1. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Is it possible to create a tool bar (example code below):
    Code (csharp):
    1. var toolbarInt = 0;
    2. var toolbarStrings : String[] = ["Toolbar1", "Toolbar2", "Toolbar3"];
    3.  
    4. function OnGUI () {
    5.     toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings);
    6. }
    except allow no buttons to be active? In other words, have 4 buttons (toolbarInt = 0, 1, 2, 3) but only have 3 visible?

    What I want is to have a row of buttons that only one or none of the buttons can be selected at any given moment.

    Only way I can think of doing this is with GUI.Toggle and a script to check for a true boolean value.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you pass in a value that's out of range, like -1, then none of the buttons will be active.

    --Eric
     
  3. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Thanks Eric! :)
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I'm trying to make a toolbar that allows you to change your selection to nothing by clicking a button again. Is this possible without writing a custom GUI.Toolbar?
     
  5. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Yes. Just set toolbarInt to -1 when the user clicks the button.
     
  6. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    If you mean deselect via clicking the currently selected toolbar item again, you'll basically have to catch the mouse down event (if (Event.current.type == EventType.MouseDown)) and check it against the screen rect for your toolbar (rect.Contains(...)), and if the click was within the toolbar and the selected index hasn't changed since the last GUI update, then set it to -1.

    -Jeremy
     
  7. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    At this point it could be nicer to do your own custom toolbar component - if you need more non-custom features added later. Could be based on something like this:

    Code (csharp):
    1. public static int MyCustomToolbar( string[] labels, int selection )
    2. {
    3.     for( int i = 0; i < labels.Length; i++ )
    4.     {
    5.         if( GUILayout.Button( labels[ i ], ( selection == i ) ? GUI.skin.GetStyle( "Button" ) : SelectedStyle )
    6.         {
    7.             if( selection != i )
    8.             {
    9.                 selection = i;
    10.             }
    11.             else
    12.             {
    13.                 selection = -1;
    14.             }
    15.         }
    16.     }
    17.  
    18.     return selection;
    19. }
     
  8. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Cool, I'm going to try that out. I wasn't sure how to do the selected look, so that's a good start for making a custom toolbar.
     
  9. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    You could just hack it from the existing styles. Something like this:
    Code (csharp):
    1.         private static GUIStyle buttonUpStyle;
    2.  
    3.         public static GUIStyle ButtonUpStyle
    4.         {
    5.             get
    6.             {
    7.                 if( buttonUpStyle == null )
    8.                 {
    9.                     buttonUpStyle = new GUIStyle( GUI.skin.GetStyle( "Button" ) );
    10.                     buttonUpStyle.name = "ButtonUp";
    11.                     buttonUpStyle.normal = buttonUpStyle.active;
    12.                     buttonUpStyle.hover = buttonUpStyle.active;
    13.                     buttonUpStyle.onNormal = buttonUpStyle.active;
    14.                     buttonUpStyle.onHover = buttonUpStyle.active;
    15.                     buttonUpStyle.onActive = buttonUpStyle.active;
    16.                     buttonUpStyle.focused = buttonUpStyle.active;
    17.                     buttonUpStyle.onFocused = buttonUpStyle.active;
    18.                 }
    19.                 return buttonUpStyle;
    20.             }
    21.         }
    Edit:
    Which would make the line
    Code (csharp):
    1. if( GUILayout.Button( labels[ i ], ( selection == i ) ? GUI.skin.GetStyle( "Button" ) : SelectedStyle )
    Look like this:
    Code (csharp):
    1. if( GUILayout.Button( labels[ i ], ( selection == i ) ? GUI.skin.GetStyle( "Button" ) : ButtonUpStyle )
     
  10. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    You can easily do it by checking if the UI changed when the selection doesn't.

    Code (CSharp):
    1.  
    2. void OnGUI()
    3. {
    4.             EditorGUI.BeginChangeCheck();
    5.             int selectedTool = GUILayout.Toolbar(_selectedTool, _toolBarIcons);
    6.             bool changed = EditorGUI.EndChangeCheck();
    7.  
    8.             if (_selectedTool != selectedTool)
    9.             {
    10.                 _selectedTool = selectedTool;
    11.                 //Do something since the selected button changed
    12.             }
    13.             else if (changed)
    14.             {
    15.                 // A click occurred on a button that was already selected. Reset the tool selection.
    16.                 _selectedTool = -1;
    17.             }
    18. }
    19.  
     
    metal6_6_6 likes this.