- Home /
Input.mousePosition not working in edit mode
I'm trying to extend the Unity editor to recognize when the mouse is hovering over a particular collider during edit mode. To do this, I've attached a method to the EditorApplication.update event (within an Editor class) and, within that method, I'm casting a ray based on the mouse position (Input.mousePosition). Unfortunately, the mousePosition is always the same value no matter where my mouse travels. I'm presuming this is because Input.mousePosition isn't updated during edit mode.
Is there a good way to get the mouse position while in edit mode?
Answer by violgamba · Sep 25, 2021 at 05:39 PM
I just worked it out. The mouse position in the scene view can be gathered within an Editor class with this code:
public void OnSceneGUI()
{
_mousePosition = Event.current.mousePosition;
}
I fact, if you just need to react to mouse position, then OnSceneGUI gets updated while the mouse is moving, rather than continuously, so it seems more efficient than using EditorApplication.update.