Skip to main content
2 of 2
replaced http://stackoverflow.com/ with https://stackoverflow.com/

Unity 4.6 UI doesn't really like those OnMouse events, so we can't use OnMouseOver() for example. (Those are just for Colliders or GUIElements.) Instead, we use Pointer properties from Selectable under EventSystem. You can add the following script on your Canvas, or individually on UI transforms, or just attach EventListeners to them.

using UnityEngine;
using UnityEngine.EventSystems; //required for Event data

public class MouseOver : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {

    GameObject currentHover;

    public void OnPointerEnter(PointerEventData eventData) {
        if (eventData.pointerCurrentRaycast.gameObject != null) {
            Debug.Log("Mouse Over: " + eventData.pointerCurrentRaycast.gameObject.name);
            currentHover = eventData.pointerCurrentRaycast.gameObject;
        }
    }

    public void OnPointerExit(PointerEventData eventData) {
        currentHover = null;
    }

    void Update() {
        if (currentHover)
            Debug.Log(currentHover.name + " @ " + Input.mousePosition);
    }
}
Zael
  • 131
  • 4