How to get mouseposition in scene view?

I tested, lnput.mousePosition only work in gameview weather edit or run mode.

I haven’t tried either, but these might help:

http://forum.unity3d.com/threads/41585-Getting-Mouse-Coordinates-in-editor-mode

1 Like

Thanks,I’v got it;
First I do a crazy thing
use user32.dll API,Find the window handler,then get the client rect and globale mous postion. at last it works!
but use
Editor.OnSceneGUI() ,
Event.current,
are vary easy to reach this.

Bump, how did you manage to do this? Literally zero info of this on interwbz:(

I know this thread is pretty old, but if someone happens to run into this problem, this might help. I found several solutions, but none worked for me.
I created

void OnSceneGUI(SceneView sceneView)
{   
   Vector3 mousePosition = Event.current.mousePosition;
   mousePosition.y = sceneView.camera.pixelHeight - mousePosition.y;
   mousePosition = sceneView.camera.ScreenToWorldPoint(mousePosition);
   mousePosition.y = -mousePosition.y
}

This did the trick, finally.

12 Likes

Wuzibu’s method was the best for me, but I had some issues with Reflection logging errors in the console in Unity 5.2.1.

Here is a fix

void OnSceneGUI()
{  
   Vector3 mousePosition = Event.current.mousePosition;
   mousePosition.y = SceneView.currentDrawingSceneView.camera.pixelHeight - mousePosition.y;
   mousePosition = SceneView.currentDrawingSceneView.camera.ScreenToWorldPoint(mousePosition);
   mousePosition.y = -mousePosition.y
}
4 Likes

what kind of script does this go in? editor script? I cant seem to get it to work. its saying Event.curent.mousePosition is null…

I put it in a CustomEditor script, see this - https://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector

If anyone was looking for how to drag an object regardless of where you click on the screen via an Update function
(i.e. for a player moving a ship on x and y across the screen) Feel free to use this code below simply have a variable to hold the oldMousePosition from Input.MousePosition in the ->class not in ->update;

{
Vector3 AmountToDrag = new Vector3();
if (Input.mousePresent)
{
if (Input.GetMouseButtonDown(0))//Mouse Left Pressed
{
oldMousePos = Input.mousePosition;//Set to Current MousePos for Accurate Drag on next frame
}
else if (Input.GetMouseButton(0))//Mouse Left Held
{
AmountToDrag = new Vector3(Input.mousePosition.x - oldMousePos.x,
Input.mousePosition.y - oldMousePos.y, 0);
AmountToDrag *= 2;//This is to Raise/Lower Drag Sensitivity (2 = x2, 0.5 = x1/2)
oldMousePos = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))//Mouse Left Released
{
AmountToDrag = new Vector2(0, 0);
}
}

Vector3 movement = new Vector3(AmountToDrag.x, AmountToDrag.y, 0);
}```

Edit: Hope This Helps!
1 Like

Just this will work perfectly. No need to flip or change anything in there.

Vector3 mousePosition = Event.current.mousePosition;
Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);

21 Likes

Thank you for saving my sanity. After a break from Unity I was bewildered how to do the simplest of things :stuck_out_tongue:

1 Like