Search Unity

Getting Mouse Coordinates in editor mode

Discussion in 'Scripting' started by manni1981, Feb 18, 2010.

  1. manni1981

    manni1981

    Joined:
    Feb 18, 2010
    Posts:
    15
    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");
    }
    }
     
  2. manni1981

    manni1981

    Joined:
    Feb 18, 2010
    Posts:
    15
    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");
    }
     
  3. Chris Morris

    Chris Morris

    Joined:
    Dec 19, 2009
    Posts:
    259
    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
     
    eDmitriy and Ali_V_Quest like this.
  4. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    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);
     
  5. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
  6. unity_44uTLDja5uCq3g

    unity_44uTLDja5uCq3g

    Joined:
    Oct 13, 2020
    Posts:
    1
    SceneView.onSceneGUIDelegate += OnSceneGUI;
    where should i write this ?
    My editor script doesnt have a Start or awake function allowed ..
    inside the Editor Constructor?
     
  7. MarkL_FRG

    MarkL_FRG

    Joined:
    Oct 3, 2022
    Posts:
    3
    for an EditorWindow, put it in static void Init()
    for a menu command, put it in your DoSomething() (see first post)