Archive for March, 2016

Unity: Get UI element under mouse

Wednesday, March 30th, 2016

To check if the mouse is over any UI element you can use

EventSystem.current.IsPointerOverGameObject()

In case that function isn’t acting to your liking and you need to debug, or if you just want to know what object is under the mouse, you can use

PointerEventData pointerData = new PointerEventData(EventSystem.current) {
	position = Input.mousePosition
};

List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerData, results);

results.ForEach((result) => {
	Debug.Log(result);
});