Is it possible to check if GUI.Label has been clicked?

Hey there,

Is it possible? Or are Buttons the only way to get it to work?

Thank you very much for any help :slight_smile:

bump, so it’s not possible?

it is possible. You can always check where the click happened and if it was within a certain area.

1 Like

Oh sweet thanks for the tip!

if you want to check for clicks, using a button thought might be a more forward solution

It’s just I’m doing a Game Menu through code and I’m using Texture2D for my custom buttons, I’m not entirely sure if this is exactly the right way to go but I’m still a complete noob when it comes to unity :smile:

I just found out there is a HitTest method however, my textures are displayed through code and it only works if the textures are added to the Hierarchy and not through code … I probably confused everyone on here so here is the code:

private var test : GUILayer;

private var isGameMenuActive : boolean = false;

var inGMenuButton : Texture2D;
var inGameMenu : Texture2D;
var inGMenuReturnButton : Texture2D;
var inGMenuHelpButton : Texture2D;
var inGMenuOptionsButton : Texture2D;
var inGMenuQuitButton : Texture2D;

function Update() {
if (Input.GetKeyDown (“escape”)) {
if (!isGameMenuActive) {
isGameMenuActive = true;
} else {
isGameMenuActive = false;
}
}
}

function OnGUI() {
// In-Game Menu
if (isGameMenuActive) {
GUI.BeginGroup(Rect(Screen.width / 2 - 295, Screen.height / 2 - 260, 590, 520));
GUI.Label(Rect(0,0,590,520),inGameMenu);
GUI.Label(Rect(160,64,287,68),inGMenuReturnButton);
inGMenuReturnButton.name = “return”;
GUI.Label(Rect(149,172,299,68),inGMenuHelpButton);
inGMenuHelpButton.name = “help”;
GUI.Label(Rect(149,280,299,68),inGMenuOptionsButton);
inGMenuOptionsButton.name = “options”;
GUI.Label(Rect(150,388,300,71),inGMenuQuitButton);
inGMenuQuitButton.name = “quit”;
GUI.EndGroup();
}
}

function OnMouseUp() {
test = Camera.main.GetComponent(GUILayer);
if(test.HitTest(Input.mousePosition) != null) {
print(test.HitTest(Input.mousePosition).name);
}
}

Edit: OnMouseUp not OnMouseDown

hittest is often used together with GUITexture. HitTest + mousedown check = button.

With ongui you have specific gadgets that offer that like the Button you can just use for example which will do exactly that

Is it possible to make a texture and use the GUI.Button for it but removing the dark grey colour it has by default?

Edit: Please ignore I managed to get the texture on the button removed the grey default colour the buttons have. Thanks again for the help dreamora :slight_smile:

it is possible to check if Input.MousePosition is within a given rectangle (Rect.Contains) The problem is that you need to specify the coordinates of the rectangles (that represent your buttons) manully which might be tedious. The buttons in this game for example are done this way

Anyway, if OnGUI works for you, then this is a lot simpler.

This detail information also very needed for me. Now It is possible to check in GUI.Label easily. Before reading your post I have 't idea about it, keep sharing.

The simplest solution to your case would be to use a button with a texture on it which is rendered like a label with a texture on it. Like so:

if (GUI.Button (Rect (160,64,287,68), inGMenuReturnButton, GUI.skin.GetStyle ("Label")))
{
// ...
}

The other option being described would be to check for mouse clicks inside the rect used to render the label:

Rect buttonRect = Rect (160,64,287,68);
GUI.Label (buttonRect, inGMenuReturnButton);
if (Event.current.type = EventType.MouseDown  buttonRect.Contains (Event.current.mousePosition))
{
// ...
    Event.current.Use ();
}

Just for kicks, this is how you would do the same with GUILayout:

GUILayout.Label (inGMenuReturnButton);
if (Event.current.type = EventType.MouseDown  GUILayoutUtility.GetLastRect ().Contains (Event.current.mousePosition))
{
// ...
    Event.current.Use ();
}

How would you check if your controls do not have specific Rect?
Like when you are using GUILayout and Horizontal/Vertical layout?

5 year old necro

Sorry for being necromancer but it seem it was needed. There was no example for this in the post.

1 Like

This is a prime example of why to start a new thread instead of necroposting, actually: this now belongs in a different subforum that didn’t exist when this question was first asked.

You’re likely looking for GUILayoutUtility.GetLastRect. Example use:

GUILayout.BeginHorizontal ();
    // More GUILayout stuff
GUILayout.EndHorizontal ();

if (Event.current.type == EventType.MouseDown && GUILayoutUtility.GetLastRect ().Contains (Event.current.mousePosition))
{
    // Do stuff
}

Moved the thread to the new forum section.

1 Like