The question is answered, right answer was accepted.
(4.6 UI) How to detect mouse over on button?
How can you call a function from a script when a button is moused over?
I tried OnMouseEnter/Exit, even WITH a collider attached to the button. I also dont see any way to make it work like on CLICK does in the inspector, although that would be very convenient!
Answer by HarshadK · Sep 02, 2014 at 06:40 AM
Method:
Add an Event Trigger component to your button game object.
Click on Add New button and select PointerEnter.
Now click on '+' button to add a new item to the list of event of type PointerEnter(BaseEventData).
Select the object containing your script.
Now select the function to be called from the list of functions.
This seems to be the cleanest way to do so, but if you have stacked objects in your GUI then this will only trigger on the top-most object. In the case of stacked objects you can use the RectTransform to deter$$anonymous$$e the world coordinates of the bounding box and take action as appropriate from there.
This is god solution, thanks a lot. I was getting worried there was no way around making your own fishy routine in Update(), or something similar...
Answer by RKSandswept · Oct 12, 2014 at 03:23 AM
Here is my solution. I fire a ray throught the UI and see what it hits. If it is a button etc. then I am in "Entering Text" mode which also covers hovering over a buttone and such. I also check the EventSystem.current.currentSelectedGameObject in case they are entering text in a field and have moved the mouse out of the field.
I also have a canvas called BackgroundEventCatcher which catches button click that get through the UI. I also have a class for showing notifications on the bottom edge of the screen.
So here it is, (Unity3d 4.6 rev 20) ....
[CODE]
PointerEventData pe = new PointerEventData(EventSystem.current);
pe.position = Input.mousePosition;
List<RaycastResult> hits = new List<RaycastResult>();
EventSystem.current.RaycastAll( pe, hits );
bool hit = false;
GameObject hgo = null;
string gos = "gos: ";
foreach(RaycastResult h in hits)
{
GameObject g = h.go;
gos += g + " ";
hit = ( g.name != "BackgroundEventCatcher" &&
(g.GetComponent<Button>() ||
g.GetComponent<Canvas>() ||
g.GetComponent<InputField>())
);
if(hit)
{
break;
}
}
if( hit ||
EventSystem.current.currentSelectedGameObject != null &&
EventSystem.current.currentSelectedGameObject.name != "BackgroundEventCatcher" &&
(EventSystem.current.currentSelectedGameObject.GetComponent<Button>() != null ||
EventSystem.current.currentSelectedGameObject.GetComponent<Canvas>() != null ||
EventSystem.current.currentSelectedGameObject.GetComponent<InputField>() != null) )
{
keyMode = eKeyInputMode.EnteringText;
}
else
{
keyMode = eKeyInputMode.KeysAreCommands;
}
Notifications.ShowNotification("hit : " + hit + " of " + hits.Count + " : " + keyMode + " : gos " + gos);
[/CODE]
Nice! I was just looking for a way to fire custom RayCasts to the ui
Just wanted to say thank you! I used it to prevent the player to move a gameObject when he was click on a UI!
Answer by srazu · Dec 31, 2014 at 07:10 PM
var EventSystem : UnityEngine.EventSystems.EventSystem;
function Start () {
EventSystem = GameObject.Find("EventSystem").GetComponent(UnityEngine.EventSystems.EventSystem);
}
function MyClickFunction () {
if (EventSystem.IsPointerOverGameObject())
return;
else
DoAnythingInTheWorld();
}
Also note that you can simply call
if(EventSystem.current.IsPointerOverGameObject())
if it's just going to be in event-based code. Though you should obviously cache it like you're doing if you're going to have it Update() somewhere.