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!
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 determine the world coordinates of the bounding box and take action as appropriate from there.
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.
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.
thank you! this is the best solution.
– ispeelgoodThis. Thank you greatly!
– perchikThis 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 determine the world coordinates of the bounding box and take action as appropriate from there.
– 3ddaveThis 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...
– TheLagbringerAnother thank you. Big help.
– Jeff_B