Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Toolbar with no selection?

Discussion in 'Extensions & OnGUI' started by bigkahuna, Jan 27, 2009.

  1. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,430
    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:
    31,781
    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,430
    Thanks Eric! :)
     
  4. Daniel-Brauer

    Daniel-Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,334
    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 Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,042
    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,334
    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 Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,042
    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:
    128
    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.