4.6 - How to detect if any GUI element has been clicked on

I know how to assign an On Click method to a button, etc… What I want to know, is how to detect if any GUI element has been clicked on.

For example, if I click somewhere (not on a GUI element), I want the player’s target to reset, but if I clicked on some GUI element - a button, a text field, etc I want the target to stay as it is.

Thanks in advance!

Get the EventSystem component that exists in your scene stored.

then:

eventSystem.currentSelectedObject will either be == the GameObject instance last clicked or null

2 Likes

Interesting to note, it also stores lastSelectedObject as well. I’m guessing you could force them back into an element quasi “modal” if currentSelectedObject goes null and set it back with lastSelectedObject.

Thanks, I’ve also found a post by Tim C where he suggests IsPointerOverEventSystemObject. So I’m gonna use smth like this (it works):

using UnityEngine.EventSystems;
void Update () {

        if (Input.GetMouseButtonDown(0) )
        {
            if (EventSystemManager.currentSystem.IsPointerOverEventSystemObject())
                Debug.Log("left-click over a GUI element!");

            else Debug.Log("just a left-click!");
        }

    }
2 Likes

you can also add an “Event > Event Trigger” component. then “add new > Pointer Click” and you get the same list as the onClick list of a button.

5 Likes

Thanks as the Above code has got me started.
How does one detect a specific button press? Say I have 3 buttons that have individual functions.

You can add a callback via the editor inspector, or via code. See the documentation for UnityEvent if you want more information.

As Tim C explained in this post: http://forum.unity3d.com/threads/button-action-with-arguments.263613/

You can do it like so ( UnityScript ) :

var iconT = (Transform)Instantiate(iconPrefab);
var button = iconT.GetComponentInChildren<Button>();
button.onClick.AddListener( function() {FunctionToCall(myargument);} );

So before I saw this message I managed to do it the following way.

I have my three buttons - I have a single script attached to each. The script checks for the object name its attached to so

if(gameObject.name == "PlayButton")
{
//Play Button Code
}
else if(gameObject.name == "MiscButton")
{
//Misc Code
}

I then added a onclick event to the button UI and attached the object, then I could use the dropdown to find the function I wanted. It needed to be public but I’m sure Serialized will work too.

1 Like

Not bad!

it’s only work in Standalone Input Module class … should check Allow Activation on Mobile Device property to force it work on touch devices ( where Touch Input Module expected to operate correctly ).

1 Like

This post saved me HOURS of greif, thank you all!

And now this is not working - the Beta 19 update , they removed access to the EventSystemManager.currentSystem.IsPointerOverEventSystemObject() and you can only do EventSystem.GameObject now … and this technique does not seem to like that.

        if (Input.GetMouseButton(0)) {
            if (eventSystem.IsPointerOverGameObject()){
                //do something
          
            } else {
                //do something else
            }
        }

Maybe the guys who removed this in Beta 19 should have read the Frequently Asked Questions and see that people were actually using EventSystemManager ! doh!

Could be there’s another way but I’m very new to Unity and C# so it was not immediately apparent to me.

EDIT: [SOLVED] the recompile after updating had removed a public reference to the event system causing the problem (so my error)

I did this to make hot key commands get ignored if edting in a text field…

        if( EventSystem.current.currentSelectedGameObject != null &&
          (EventSystem.current.currentSelectedGameObject.GetComponent<Text>() != null ||
           EventSystem.current.currentSelectedGameObject.GetComponent<InputField>() != null) )
        {
            keyMode = eKeyInputMode.EnteringText;
        }
        else
        {
            keyMode = eKeyInputMode.KeysAreCommands;
        }

Then in code that tests keys I do this…

if(GodClass.keyMode == eKeyInputMode.KeysAreCommands && Input.GetKeyDown(KeyCode.Delete))
1 Like

Can someone kindly summarize which solution works in b20? IsPointerOverEventSystemObject or IsPointerOverGameObject or something else?
Thanks a Lot

IsPointerOverGameObject might not be what you are looking for since it will be true if you are over a non-GUI GameObject as well.

I’m going to try to test IsPointerOverEventSystemObject now.

EDIT 1: IsPointerOverEventSystemObject is no longer available through the vanilla system that Unity provides.

EDIT2: Adding an EventTrigger to all my buttons to detect mouse enter is a potential option. Unfortunately that seems to break drag-scrolling in ScrollRect so you have to add the EventTrigger to the ScrollRect itself, not the items inside it.

Has anyone figured out how to do this in b19+?

Can anyone share some example project where we can touch those GUI elements?
I’m not sure where to put those codes and when.

I want to figure out if any GUI-Object has been clicked/touched/changed and what object it is:
EventSystem is unknown identifier (b15).
Where is the scripting API for the “EventSystem”?

Here is the EventSystem documentation: Redirecting to latest version of com.unity.ugui

What I ended up doing is putting MouseEnter and MouseExit triggers on all my GUI elements that I wanted to track of. Then I keep a reference to what object the cursor is over. Unfortunate that it is not built into Unity but it is pretty powerful when you get a hang of it since you can build dependencies and layers with a custom system.

So far it is working out well for me.

1 Like