Search Unity

Check if any button is pressed

Discussion in 'Immediate Mode GUI (IMGUI)' started by Ithkul, Sep 27, 2014.

  1. Ithkul

    Ithkul

    Joined:
    Apr 8, 2012
    Posts:
    22
    Hi

    I have a challenge with pressing through buttons and into the gameworld behind the GUI. So I would like to make sure that no buttons are pressed. Both existing buttons and future buttons I might implement. I have several seperate classes working with both OnGUI and CrossPlatformInput

    Code (csharp):
    1.  
    2. //make sure no crossplatform buttons are pressed
    3. if(!CrossPlatformInput.GetButtonDown(All buttons)) {
    4.  
    5.   //make sure no standard buttons are pressed
    6.   if(!GUI.Button(All buttons)) {
    7.      //here I will do my gameworld input checks.
    8.  
    9.   }
    10. }
    11.  
    Any light or brainstorming on this topic would be most welcome.

    Best regards,
    Ithkul
     
  2. Ithkul

    Ithkul

    Joined:
    Apr 8, 2012
    Posts:
    22
    So no one has this problem? Or no one knows a solution?

    Best regards,
    Daniel
     
  3. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Hm, is it for mobile or desktop?

    I think a good starting point would be the TouchInputModule (http://docs.unity3d.com/460/Documentation/Manual/script-TouchInputModule.html, http://docs.unity3d.com/460/Documentation/ScriptReference/EventSystems.TouchInputModule.html). Not quite sure though, I haven't worked a lot with it. I have a similar problem, with GUI elements, behind other GUI elements, that keep being clickable, even though they can't be seen. My solution was to just deactivate the objects, but I guess that is not a possibility in your case.
     
  4. AdamScura

    AdamScura

    Joined:
    Mar 25, 2012
    Posts:
    55
    Let me see if I understand you correctly. The game world should only respond to the mouse down event if the user has not clicked a GUI button. The problem is that your game world is responding to every click, even when the user clicks on a GUI button.

    Try this:

    Code (CSharp):
    1. void OnGUI()
    2. {
    3.     if (GUI.Button(new Rect(20, 20, 200, 40), "Button 1"))
    4.     {
    5.         Debug.Log("Button MouseDown");
    6.     }
    7.  
    8.     // GUI.Button() "eats" MouseDown and MouseUp events, changing them into Used events.
    9.     // So if a GUI button was pressed before this line, Event.current.type will be 'Used'.
    10.     // If the mouse did not fall on a GUI button, the Event.current.type will still be 'MouseDown'.
    11.     if (Event.current.type == EventType.MouseDown)
    12.     {
    13.         if (CrossPlatformInput.GetButtonDown("Fire1"))
    14.         {
    15.             Debug.Log("Fire!");
    16.         }
    17.     }
    18. }
    Be careful with this approach because it only works with MouseDown, and totally defeats the purpose of using CrossPlatformInput. You might as well use Input.GetButtonDown().

    I really suggest you read the documentation for the Legacy GUI and the new Unity UI.

    Legacy GUI (the one you are using): http://docs.unity3d.com/Manual/gui-Basics.html
    The new Unity UI: http://docs.unity3d.com/Manual/UISystem.html

    Good luck!
     
    Last edited: Dec 15, 2014