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.