(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!

Method:

  1. Add an Event Trigger component to your button game object.
  2. Click on Add New button and select PointerEnter.
  3. Now click on ‘+’ button to add a new item to the list of event of type PointerEnter(BaseEventData).
  4. Select the object containing your script.
  5. Now select the function to be called from the list of functions.

var EventSystem : UnityEngine.EventSystems.EventSystem;

function Start () {
	EventSystem = GameObject.Find("EventSystem").GetComponent(UnityEngine.EventSystems.EventSystem);
}

function MyClickFunction () {
	if (EventSystem.IsPointerOverGameObject())
		return;
	else
		DoAnythingInTheWorld();
}

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) …

        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);