Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

How To Get The Editor Tools Button Styles?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Dreamteck, Apr 21, 2019.

  1. Dreamteck

    Dreamteck

    Joined:
    Feb 12, 2015
    Posts:
    211
    Is there a way to get a reference to the editor style of the Tool buttons that the Unity editor uses?
    editorbuttonstyles.PNG

    The closest I have seen so far is:

    Code (CSharp):
    1. EditorStyles.miniButtonLeft;
    2. EditorStyles.miniButtonMid;
    3. EditorStyles.miniButtonRight;
    4.  
    But these button styles are shorter than the ones in the toolbar of the editor. I think that the same button style is used in the Terrain inspector.
     
  2. Dreamteck

    Dreamteck

    Joined:
    Feb 12, 2015
    Posts:
    211
    Welp, here is a workaround to get a somewhat similar behavior to the desired one:
    upload_2019-4-23_16-47-19.png

    We get the button style and remove the horizontal margins:
    Code (CSharp):
    1. buttonStyle = new GUIStyle(GUI.skin.button);
    2.             buttonStyle.margin = new RectOffset(0, 0, buttonStyle.margin.top, buttonStyle.margin.bottom);
    Then we do this:

    Code (CSharp):
    1. bool LeftButton(string title)
    2.         {
    3.             bool clicked = false;
    4.             Rect rect = GUILayoutUtility.GetRect(30f, 25f);
    5.             GUI.BeginGroup(rect);
    6.             if(GUI.Button(new Rect(0, 0, rect.width + buttonStyle.border.right, rect.height), title, buttonStyle)) clicked = true;
    7.             GUI.EndGroup();
    8.             return clicked;
    9.         }
    10.  
    11.         bool MidButton(string title)
    12.         {
    13.             bool clicked = false;
    14.             Rect rect = GUILayoutUtility.GetRect(30f, 25f);
    15.             GUI.BeginGroup(rect);
    16.             if (GUI.Button(new Rect(-buttonStyle.border.left, 0, rect.width + buttonStyle.border.left + buttonStyle.border.right, rect.height), title, buttonStyle)) clicked = true;
    17.             GUI.EndGroup();
    18.             return clicked;
    19.         }
    20.  
    21.         bool RightButton(string title)
    22.         {
    23.             bool clicked = false;
    24.             Rect rect = GUILayoutUtility.GetRect(30f, 25f);
    25.             GUI.BeginGroup(rect);
    26.             if (GUI.Button(new Rect(-buttonStyle.border.left, 0, rect.width + buttonStyle.border.left, rect.height), title, buttonStyle)) clicked = true;
    27.             GUI.EndGroup();
    28.             return clicked;
    29.         }
    And finally call the buttons:

    Code (CSharp):
    1.             EditorGUILayout.BeginHorizontal(GUILayout.Width(160f));
    2.  
    3.             if (LeftButton("M"))
    4.             {
    5.  
    6.             }
    7.  
    8.             if (MidButton("R"))
    9.             {
    10.  
    11.             }
    12.  
    13.             if (MidButton("S"))
    14.             {
    15.  
    16.             }
    17.  
    18.             if (RightButton("N"))
    19.             {
    20.  
    21.             }
    22.  
    23.             EditorGUILayout.EndHorizontal();
    Absolute horror show and you don't get the nice borders in between the buttons but at least it's something.
     
  3. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    712
  4. Dreamteck

    Dreamteck

    Joined:
    Feb 12, 2015
    Posts:
    211