Getting Mouse Coordinates in editor mode

Dear forum members,

i’m trying to write a small script to place the selected object using getting the mouse coordinates, transforming it into a ray using the editor cam, casting a ray in the scene and placing it to the hitresult if existing.
my main problem here is that Input.mousePosition always gives me 0/0/0 - anyone has any idea why this doesn’t work in editor or how to get the correct mousePosition?

thx very much
manni

here is my current code


@MenuItem (“GameObject/Place %i”)
static function DoSomething () {
if (Selection.activeGameObject)
{
var cPosition = Selection.activeGameObject.transform.position;

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// get intersection between ray and physics
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 1000.0))
{
Selection.activeGameObject.transform.position = hit.point;
print(“Placed Object to” + hit.point.x + “/” + hit.point.y + “/” + hit.point.z);
}
Debug.Log (“Perform operation”);
}
}

okay, my script was trash anyway, i’ve updated it now to place the selected element into the middle of the current view, casting a ray into the scene. this works fine, but still the previous problem of getting the mouse coordinates exists, seems i’m too stupid to check how the those…

@MenuItem (“GameObject/Place %i”)
static function DoSomething () {
var currentWindow = EditorWindow.mouseOverWindow;
// print("CurrentEditorwindow name: " + currentWindow.name + “/” + currentWindow.GetInstanceID());

// var currentCamera = Camera.current;
// print("pixelRect " + currentCamera.pixelRect.x + “/” + currentCamera.pixelRect.y + “/” + currentCamera.pixelRect.width + “/” + currentCamera.pixelRect.height);

print("MouseCoordCurrent: " + Input.mousePosition.x + “/” + Input.mousePosition.y + “/” + Input.mousePosition.z);

if (Selection.activeGameObject Camera.current)
{
var cPosition = Selection.activeGameObject.transform.position;

var mousePosition : Vector3;
mousePosition.x = Camera.current.pixelRect.width/2.0;
mousePosition.y = Camera.current.pixelRect.height/2.0;
mousePosition.z = 0.0;

// Input.mousePosition not working, how can i get that mouseposition in editor mode?
// var ray = Camera.current.ScreenPointToRay (Input.mousePosition);

var ray = Camera.current.ScreenPointToRay (mousePosition);
// get intersection between ray and physics
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 1000.0))
{
Selection.activeGameObject.transform.position = hit.point;
print(“Placed Object to” + hit.point.x + “/” + hit.point.y + “/” + hit.point.z);
}
else
print(“no collision found”);
}
else
print(“no selected gameobject or current camera found”);
}

To get the mouse position you have to listen for an event inside of an OnInspectorGUI, which can be accessed through an editor script (there is information how to do this).

Then get your mouse coordinates by using Event.mousePosition.

To convert these to your editor’s window, you have to account for the y axis flip, and the extra space around your editor window.

This will look something like this:

newMousePosition.y = Screen.height - (currentEvent.mousePosition.y + 25);

25 is an approximated difference that accounts for the part of the Unity outside of the editor window.

Hope this helps,
Chris

2 Likes

You can attach to the event:
SceneView.onSceneGUIDelegate += OnSceneGUI;

Then write your own function:
private void OnSceneGUI(SceneView sceneView)

Inside, take the mousePos using Event.current, instead of Input, which is disabled in Editor mode.

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

No need to flip Y.

Others say using ScreenPointToRay instead. This happens in other scenarios.

In that case you have to flip Y.

Vector3 mousePosition = Event.current.mousePosition;
mousePosition.y = sceneView.camera.pixelHeight - mousePosition.y; // Flip y
Ray ray = sceneView.camera.ScreenPointToRay(mousePosition);

4 Likes

Also note if you’re using Windows and the scale isn’t 100%, you will have to multiply Event.current.mousePosition by 1.25f if you’re using 125%, etc.

SceneView.onSceneGUIDelegate += OnSceneGUI;
where should i write this ?
My editor script doesnt have a Start or awake function allowed …
inside the Editor Constructor?

for an EditorWindow, put it in static void Init()
for a menu command, put it in your DoSomething() (see first post)