Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

The mousePosition in the editor is not correct when use 4K screen.

Discussion in 'Editor & General Support' started by watsonsong, Jul 9, 2018.

  1. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I add a OnSceneGUI method to the SceneView.onSceneGUIDelegate, and add following code to covert the mouse click to world position in a 2D game:
    `
    var e = Event.current;
    if (e.type == EventType.MouseUp &&
    e.button == 1 &&
    e.modifiers == EventModifiers.Control)
    {
    var cam = sceneView.camera;
    var screenPos = new Vector3(
    e.mousePosition.x,
    cam.pixelHeight - e.mousePosition.y,
    cam.nearClipPlane);
    var worldPos = cam.ScreenToWorldPoint(screenPos);
    // Do something
    }
    `

    The code work fine on 1080p monitor, but the world position is not correct on a 4K screen.
     
  2. grimjim

    grimjim

    Joined:
    Jan 18, 2014
    Posts:
    17
    same here. could you solve it?
     
  3. jura_z

    jura_z

    Unity Technologies

    Joined:
    Oct 10, 2016
    Posts:
    27
    Since I faced this issue by myself and google showed me this thread...
    So the solution is HandleUtility.GUIPointToWorldRay instead of ScreenToWorldPoint
    That's because Event's mousePosition is not in the world coordinates, but in some GUIPoint coords. Hope this helps
     
    TomEkblomThunderful likes this.