- Home /
Capturing Mouse Position w/o Polling Every Update?
Hey everyone,
For my GUI, I'd like to use keep track of the mouse position while avoiding simply polling for the position every Update(). Ideally, I'd like to leverage the "mouseMove" event since then I can unnecessary code execution when the mouse position hasn't changed, but the mouseMove Event isn't fired in game (only the editor, see http://unity3d.com/support/documentation/ScriptReference/EventType.MouseMove.html).
Does anyone have a good workaround for this problem? Is there a efficient, standard way to capture mouse position that I'm just overlooking?
Thanks,
Nate
I can't help but wonder if this isn't a little overkill? A simple Input.mousePosition poll once a frame probably isn't noticeable I$$anonymous$$HO (is it?). I'm interested in any performance improvement this thread might bring but my two cents is 'just get it if you need it' :)
I agree with BY0LOG1C. If your problem is a high-cost operation you need to do each move, then compare position to last frame and avoid the operation when zero change, but just the call to get the mousePosition is immaterial compared to everything else you need to do every frame.
What Warwick said needs to be made the answer. This is such an insignificant use of resources and you have far bigger things to worry about.
Answer by Waz · Sep 02, 2011 at 12:41 AM
Since your problem is a high-cost operation which you need to do each move, solve this by comparing the mousePosition to what it was in the previous frame and avoid the operation when the change is zero. The actual cost of reading mousePosition is immaterial.
Answer by SinisterRainbow · Aug 14, 2013 at 09:24 PM
This is an old question, and I agree with the answer: but there is another work around that may work for some: events are fired during MouseDrag you can catch. Read the docs on it if appropriate for you.
Answer by rocifier · Apr 28 at 10:23 AM
Update: There is now a new Input System package which could do this more generically without code.