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.
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!");
}
}
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.
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 ).
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)
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.
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”?
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.