Search Unity

How to create empty a button with specified size, no border, no style.

Discussion in 'Immediate Mode GUI (IMGUI)' started by eses, Sep 24, 2020.

  1. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi,

    I can't find anything in my notes about this and I'm going to continue testing this, but how could I create a button that:

    - Has a specified size
    - Has no border around it, so that two buttons have no gap between them
    - Has no button style (I'll render other stuff over it)

    I've used this for now:
    Code (CSharp):
    1. if (GUILayout.Button("", GUILayout.Width(tileSize), GUILayout.Height(tileSize)))
    2. {
    3. // ...
    4. }
    But this leaves gaps between buttons.

    And I haven't quite figured it out, can / should I use GUIStyle or GUIContent or something...

    Reason for this - this looks like the easiest way to create clickable region... this is going into a Editor Window. I'll render stuff over the button later.

    Another issue, I can't get mouse down, only click so I could perhaps do some mouse event checks against rectangles I guess.
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    OK, I think I'll go with this...

    Create some sort of function that spits out a style:
    Code (CSharp):
    1. static GUIStyle GetStyle()
    2. {
    3.     GUIStyle style = new GUIStyle(GUI.skin.label);
    4.     style.margin = new RectOffset();
    5.     style.fixedHeight = 24;
    6.     style.fixedWidth = 24;
    7.     return style;
    8. }
    And then I use it like this in my button. Seems to be working so far.

    Code (CSharp):
    1. if (GUILayout.Button("", GetStyle()))
    2. {
    3.        // ...
    4. }