World mouse position in EditorWindow

Hello,

I don’t seem to be able to find the world mouse position in an EditorWindow.
Right now I feel like it is not possible at all… Here some things I tried but it always return the position at the center of the window.

void OnSceneGUI(SceneView sceneView)
{
// Camera.
Camera cam = sceneView.camera;
if (cam == null)
return;

// Update mouse grid position.
Event e = Event.current;
Vector2 mousePosition = e.mousePosition;
Vector3 mouseWorldPos = HandleUtility.GUIPointToWorldRay(mousePosition).origin;
}

OnSceneGUI and HandleUtility were not supported by EditorWindow so I had to add :

SceneView.onSceneGUIDelegate += OnSceneGUI;

To the enable call. and :

SceneView.onSceneGUIDelegate -= OnSceneGUI;

To the disable call. but still not success, it returns the center of the screen in world pos.

Also tried to to use cam.ScreenToWorldPoint but no success… The Z was set to camera near clip plane in case someone ask.

I also tried to make my own function to find the world position from the the screen space pos (between -1 and 1) but it does not work neither. It seems like the camera informations for the scene view (fov, near clip plane, matrix…) are wrong.

private Vector3 CameraSpaceToWorldPosition(Vector2 cameraSpacePos, Camera camera)
{
float halfFov = camera.fieldOfView * .5f;

float halfHeight = Mathf.Abs(Mathf.Tan(halfFov)) * camera.nearClipPlane;
float halfWidth = halfHeight * camera.aspect;

Vector3 localPos = new Vector3(halfWidth * cameraSpacePos.x, halfHeight * cameraSpacePos.y, camera.nearClipPlane);
return camera.cameraToWorldMatrix * localPos;
}

By the way I am using unity 5.5.0f3, at the point I am thinking about using something else than EditorWindow since it seems the problem is coming from there. I have been able to get the mouse position from custom editors in the past.

Thanks in advance.

1 Like

Tried to use :

Vector3 mousePosition = Event.current.mousePosition;

float mult = EditorGUIUtility.pixelsPerPoint;
mousePosition.y = sceneView.camera.pixelHeight - mousePosition.y * mult;
mousePosition.x *= mult;

Ray ray = sceneView.camera.ScreenPointToRay(mousePosition);

from VertexPaint/Editor/VertexPainterWindow_Painting.cs at master · slipster216/VertexPaint · GitHub

But still no success… I am starting to thing it mays be a bug of the 5.5.0f3 or because I am using two monitors…

2 Likes

Nevermind I am (of course) just an idiot.

The world position was right but when I projected it on the plane of my grid I used the vector forward of the camera instead of the Mouse world pos - camera position.

1 Like

I used @Fauvent 's and this is what I ended up with. Hope this helps the next person to find this thread!

using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
/// <summary>
///
/// </summary>
[ExecuteInEditMode]
public class ClickSpawn : MonoBehaviour
{
    private void OnEnable()
    {
        if (!Application.isEditor)
        {
            Destroy(this);
        }
        SceneView.onSceneGUIDelegate += OnScene;
    }

    void OnScene(SceneView scene)
    {
        Event e = Event.current;

        if (e.type == EventType.MouseDown && e.button == 2)
        {
            Debug.Log("Middle Mouse was pressed");

            Vector3 mousePos = e.mousePosition;
            float ppp = EditorGUIUtility.pixelsPerPoint;
            mousePos.y = scene.camera.pixelHeight - mousePos.y * ppp;
            mousePos.x *= ppp;

            Ray ray = scene.camera.ScreenPointToRay(mousePos);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                //Do something, ---Example---
                GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                go.transform.position = hit.point;
                Debug.Log("Instantiated at " + hit.point);
            }
            e.Use();
        }
    }
}
#endif
1 Like

i have to say thank you very much

Very helpful!

1 Like

I don’t understand the answers, how it is done from an EditorWindow ? The OnScene message method is not available there.