Posts Tagged ‘mouse’

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

Unity: Is mouse (or any coordinates) within UI element’s rect

Monday, March 30th, 2015

Here’s a quick way to check if the mouse or any other coordinates are within any UI object’s boundaries.

This method doesn’t use raycast so it ignores all overlapping objects, it works just like ActionScript’s object.HitTest(coords) function.

public bool AreCoordsWithinUiObject(Vector2 coords, GameObject gameObj)
{
	Vector2 localPos = gameObj.transform.InverseTransformPoint(coords);
	return ((RectTransform) gameObj.transform).rect.Contains(localPos);
}

// Example usage
bool isMouseOverIcon = AreCoordsWithinGameObject(Input.mousePosition, _myUiIcon);

Mouse scroll wheel in flash not working in Chrome

Tuesday, August 14th, 2012

Chrome’s latest update to version 21 introduced a new type of flash player using the PPAPI (pepper) plugin architecture.
I noticed that this version of flash started breaking mouse scroll wheel support for some flash content.

Which flash files break and which don’t depend on one thing; if you are using JavaScript to prevent the default behavior of the mouse scroll wheel or not.

The reason for preventing default mouse scroll wheel behavior is for when you don’t want the page to scroll when you scroll over the flash content.
To prevent that, developers write JavaScript code (or use libraries like SwfWheel.js) that add an event listener for the “mousewheel” or “DOMMouseScroll” event, and in the handler call “evt.preventDefault()” and/or “evt.returnValue = false” to cancel the scroll.

Therein lies the problem, the old NPAPI Flash Player would receive the mouse scroll event even if we prevented the default behavior. Maybe even incorrectly so, since the event was cancelled.
The new PPAPI Flash Player will not receive the event if the default is prevented.

If one were to remove the preventDefault() function, the Flash file would start working correctly again, but the whole page would again start scrolling, which is out of the question.

You will have to not only prevent the default scroll behavior, but also call an external interface function in your flash file to let it know when the user scrolls the mouse wheel.
In my case I was using SwfWheel and only needed a small modification to the library file: in the SWFWheel.getState() function add this code before the “if (br.mac)” line:

// The new pepper plugin requires chrome to always be hacked.
if (br.chrome)
{
	return STATE_HACKED;
}