- Home /
More elements inside GUI button
This is my inventory screen. To display items I use buttons with GUIContent(WeaponName, WeaponIcon)
. Problem is I need more stuff inside the buttons (texture Icon,string Name, texture Coin Icon,int Price,texture Stone Icon,float Weight). Is it possible to add all of those inside the button, so it's still clickable? Is there any way to tell unity WHERE inside the button to show things mentioned above? Like having the icon on the left side, not centered.
Answer by Bunny83 · Aug 13, 2011 at 11:28 PM
I don't want to encourage people to dig to deep into foreign code, but i guess the UnityEngine.dll is not a big secret since it's all managed code ;). ILSpy gives me this:
public static bool Button(Rect position, GUIContent content, GUIStyle style)
{
GUIUtility.CheckOnGUI();
int controlID = GUIUtility.GetControlID(GUI.buttonHash, FocusType.Native, position);
switch (Event.current.GetTypeForControl(controlID))
{
case EventType.MouseDown:
{
if (position.Contains(Event.current.mousePosition))
{
GUIUtility.hotControl = controlID;
Event.current.Use();
}
return false;
}
case EventType.MouseUp:
{
if (GUIUtility.hotControl == controlID)
{
GUIUtility.hotControl = 0;
Event.current.Use();
return position.Contains(Event.current.mousePosition);
}
return false;
}
case EventType.MouseDrag:
{
if (GUIUtility.hotControl == controlID)
{
Event.current.Use();
}
break;
}
case EventType.Repaint:
{
style.Draw(position, content, controlID);
break;
}
}
return false;
}
As you can see the magic behind the GUI.Button function is not black-magic. The whole GUI system is build on top of styles. Styles are almost the only things that can be drawn in the GUI system. A GUIStyle supports only 1 background image, 1 content image and the content-text. More complex GUI-elements like a Scrollview for example uses multiple styles. Each scrollbar uses 4 different styles (background, up-button, down-button, thumb). Those more complex elements just uses other simpler elements internally.
You can build more complex functions on your own. The GUI system is that simple to provide the best extensibility.
You can learn a lot about the GUI system by using ILSpy or any other .NET reflector.
Answer by Julien-Lynge · Aug 13, 2011 at 08:48 PM
Buttons are far too simple for what you're trying to do. I can't say I'm any expert at GUI, but you probably want to create a GUI Group (see GUI.BeginGroup) and then layout your group as you wish. To make it 'clickable', what I would do is, within the OnGUI() function, do something like:
Vector2 clickPos = Input.mousePosition;
clickPos.y = Screen.height - clickPos.y;
if (Input.GetMouseDown(0)) //user clicked
{
foreach(Rect button in myButtons)
{
if (button.Contains(clickPos))
//user clicked on this button, do something
}
}
so it's not possible to add more than 1 texture and 1 string inside the button?
Yes, a button is more of a primitive type than an extensible object. You could try doing creative things like painting additional labels over the button and seeing if you're still able to click the button through the labels, but the button object itself can only have 1 texture and 1 string.
I made a GUI.Label with the icon over the button and I was still able to click it normally. I guess I'll have to make an empty button and all the stuff I want over it.
GUI.BeginGroup will be your friend if you're doing that. Create a Rect to place your button, then a group using the Rect, then all the other textures and text as a percentage of this Rect, with x,y = 0,0 corresponding to the top left (or just use the layout stuff I linked earlier). That way, you can later resize your button and everything within it will resize as well.
But what is the use of GUI.BeginGroup if simply drawing a label on top of the button does the work?? Could you please add a code snippet with GUI.BeginGroup(), as I have encountered the same problem?