Search Unity

GUI tab style controls

Discussion in 'Scripting' started by chubbspet, May 12, 2011.

  1. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Hi there

    I find it surprising that there is nothing on google related to a tab-style user interface. Can anybody maybe steer me into a direction?

    Thans
     
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    A tabbed interface in unity would really just be a bunch of GUI Buttons, that activated different GUI Labels in a set area; I'd probably use an enum for that part.
     
  3. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Do you think it could work to have 3 groups next to each other and have boolean variables control which one is visible?
     
  4. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    The enum is probably better because it can have only one state at a time. Imagine a character sheet with "Inventory", "Skills", and "Quests" all together. With bools, you might have to toggle them back and forth and have the right one. With an enum, set the "Page = Pages.Skills" and use that enum to show *only* the right page instead of risking having no page set to true or multiple pages set to true.
     
  5. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    got it, thanks
     
  6. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Last edited: Aug 30, 2012
  7. vikingfabian-com

    vikingfabian-com

    Joined:
    Dec 5, 2013
    Posts:
    9
    Made one for editor GUI, looks almost like the Unity tabs:


    Code (CSharp):
    1. /// <summary>
    2. /// Creates tabs from buttons, with their bottom edge removed by the magic of Haxx
    3. /// </summary>
    4. /// <remarks>
    5. /// The line will be misplaced if other elements is drawn before this
    6. /// </remarks>
    7. /// <returns>Selected tab</returns>
    8. public static int Tabs(string[] options, int selected)
    9. {
    10.     const float DarkGray = 0.4f;
    11.     const float LightGray = 0.9f;
    12.     const float StartSpace = 10;
    13.  
    14.     GUILayout.Space(StartSpace);
    15.     Color storeColor = GUI.backgroundColor;
    16.     Color highlightCol = new Color(LightGray, LightGray, LightGray);
    17.     Color bgCol = new Color(DarkGray, DarkGray, DarkGray);
    18.  
    19.     GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
    20.     buttonStyle.padding.bottom = 8;
    21.  
    22.     GUILayout.BeginHorizontal();
    23.     {   //Create a row of buttons
    24.         for (int i = 0; i < options.Length; ++i)
    25.         {
    26.             GUI.backgroundColor = i == selected ? highlightCol : bgCol;
    27.             if (GUILayout.Button(options[i], buttonStyle))
    28.             {
    29.                 selected = i; //Tab click
    30.             }
    31.         }
    32.     } GUILayout.EndHorizontal();
    33.     //Restore color
    34.     GUI.backgroundColor = storeColor;
    35.     //Draw a line over the bottom part of the buttons (ugly haxx)
    36.     var texture = new Texture2D(1, 1);
    37.     texture.SetPixel(0, 0, highlightCol);
    38.     texture.Apply();
    39.     GUI.DrawTexture(new Rect(0, buttonStyle.lineHeight + buttonStyle.border.top + buttonStyle.margin.top + StartSpace,  Screen.width, 4),texture);
    40.  
    41.     return selected;
    42. }
     
    FM-Productions likes this.