event mouse position bug ?

Hi,

This code return me those results :

private void UpdateMousePos(Event e, EditorWindow w)
{
	mousePos.x = e.mousePosition.x / w.position.width;
	mousePos.y = 1.0f - e.mousePosition.y / w.position.height;

	if (IsPainting)
		Debug.Log("mouse position " + e.mousePosition + " window " + w.position);
}

results

Half results are wrong.

What is wrong?

The first result is relative to the current viewport (397) and the second to the screen (1351~ = 956 + 397, viewport pos + mouse pos).
Obviously the paint event is called twice, one for the viewport, one for the whole screen.
So my tools bug 1/2 time because the mouse pos I use is not the right one for the viewport used.

I made some tests. It only happens if a any key is pressed. Otherwise the mouse position is relative to the current overred window.

New code, new log, same bug:

public void OnSceneGUI()
{
	Event current = Event.current;
	mousePos = current.mousePosition;
	Debug.Log("mouse position " + mousePos + " " + current.type);
}

You should be checking Event.current.type to ensure that your code only responds to the appropriate events on each call, otherwise there’s a lot of waste in your OnGUI calls. If the position returned varies based on the event, this could be by design rather than a bug.

I’d suggest reporting a bug anyway, because the documentation (Unity - Scripting API: Event) doesn’t mention it (as far as I can tell with a 5 second look), so an update somewhere would be good.

The result is the same in MouseMove or MouseDrag.

Solved. The problem was from unity mouseposition behaviour plus a bug in my own code. Both giving similar results.
So, as you says, mouse position in MouseMove and MouseDrag is relative to current viewport.

You should file a bug requesting the documentation to clarify that point, then, so that others don’t have to figure it out for themselves in the future like you had to. :wink: