Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Immediate Mode GUI (IMGUI)' started by rubix, Sep 29, 2010.

  1. rubix

    rubix

    Joined:
    Sep 24, 2010
    Posts:
    12
    Hey there,

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

    Thank you very much for any help :)
     
  2. rubix

    rubix

    Joined:
    Sep 24, 2010
    Posts:
    12
    bump, so it's not possible?
     
  3. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    it is possible. You can always check where the click happened and if it was within a certain area.
     
    liortal likes this.
  4. rubix

    rubix

    Joined:
    Sep 24, 2010
    Posts:
    12
    Oh sweet thanks for the tip!
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    if you want to check for clicks, using a button thought might be a more forward solution
     
  6. rubix

    rubix

    Joined:
    Sep 24, 2010
    Posts:
    12
    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 :D

    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
     
    Last edited: Sep 30, 2010
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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
     
  8. rubix

    rubix

    Joined:
    Sep 24, 2010
    Posts:
    12
    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 :)
     
    Last edited: Sep 30, 2010
  9. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    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
    http://www.wooglie.com/playgame.php?gameID=107
    Anyway, if OnGUI works for you, then this is a lot simpler.
     
  10. jillyronald

    jillyronald

    Joined:
    Sep 28, 2010
    Posts:
    4
    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.
     
  11. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    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:
    Code (csharp):
    1. if (GUI.Button (Rect (160,64,287,68), inGMenuReturnButton, GUI.skin.GetStyle ("Label")))
    2. {
    3. // ...
    4. }
    The other option being described would be to check for mouse clicks inside the rect used to render the label:
    Code (csharp):
    1. Rect buttonRect = Rect (160,64,287,68);
    2. GUI.Label (buttonRect, inGMenuReturnButton);
    3. if (Event.current.type = EventType.MouseDown  buttonRect.Contains (Event.current.mousePosition))
    4. {
    5. // ...
    6.     Event.current.Use ();
    7. }
    Just for kicks, this is how you would do the same with GUILayout:
    Code (csharp):
    1. GUILayout.Label (inGMenuReturnButton);
    2. if (Event.current.type = EventType.MouseDown  GUILayoutUtility.GetLastRect ().Contains (Event.current.mousePosition))
    3. {
    4. // ...
    5.     Event.current.Use ();
    6. }
     
  12. ZammyIsOnFire

    ZammyIsOnFire

    Joined:
    Jul 7, 2014
    Posts:
    9
    How would you check if your controls do not have specific Rect?
    Like when you are using GUILayout and Horizontal/Vertical layout?
     
  13. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  14. ZammyIsOnFire

    ZammyIsOnFire

    Joined:
    Jul 7, 2014
    Posts:
    9
    Sorry for being necromancer but it seem it was needed. There was no example for this in the post.
     
    Ash-Blue likes this.
  15. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    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.
     
  16. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    You're likely looking for GUILayoutUtility.GetLastRect. Example use:

    Code (csharp):
    1. GUILayout.BeginHorizontal ();
    2.     // More GUILayout stuff
    3. GUILayout.EndHorizontal ();
    4.  
    5. if (Event.current.type == EventType.MouseDown && GUILayoutUtility.GetLastRect ().Contains (Event.current.mousePosition))
    6. {
    7.     // Do stuff
    8. }
    Moved the thread to the new forum section.
     
    ZammyIsOnFire likes this.