Search Unity

How to get mouseposition in scene view?

Discussion in 'Scripting' started by jwyoo_stone, Nov 5, 2013.

  1. jwyoo_stone

    jwyoo_stone

    Joined:
    Jul 15, 2012
    Posts:
    12
    I tested, lnput.mousePosition only work in gameview weather edit or run mode.
     
  2. rutter

    rutter

    Joined:
    Mar 21, 2012
    Posts:
    84
  3. jwyoo_stone

    jwyoo_stone

    Joined:
    Jul 15, 2012
    Posts:
    12
    Thanks,I'v got it;
    First I do a crazy thing
    use user32.dll API,Find the window handler,then get the client rect and globale mous postion. at last it works!
    but use
    Editor.OnSceneGUI() ,
    Event.current,
    are vary easy to reach this.
     
  4. JudeJohan

    JudeJohan

    Joined:
    Nov 4, 2014
    Posts:
    1
    Bump, how did you manage to do this? Literally zero info of this on interwbz:(
     
  5. wuzibu

    wuzibu

    Joined:
    Dec 15, 2014
    Posts:
    10
    I know this thread is pretty old, but if someone happens to run into this problem, this might help. I found several solutions, but none worked for me.
    I created

    Code (CSharp):
    1. void OnSceneGUI(SceneView sceneView)
    2. {  
    3.    Vector3 mousePosition = Event.current.mousePosition;
    4.    mousePosition.y = sceneView.camera.pixelHeight - mousePosition.y;
    5.    mousePosition = sceneView.camera.ScreenToWorldPoint(mousePosition);
    6.    mousePosition.y = -mousePosition.y
    7. }
    8.  
    This did the trick, finally.
     
    Last edited: Oct 3, 2015
  6. glitchers

    glitchers

    Joined:
    Apr 29, 2014
    Posts:
    64
    Wuzibu's method was the best for me, but I had some issues with Reflection logging errors in the console in Unity 5.2.1.

    Here is a fix

    Code (CSharp):
    1. void OnSceneGUI()
    2. {  
    3.    Vector3 mousePosition = Event.current.mousePosition;
    4.    mousePosition.y = SceneView.currentDrawingSceneView.camera.pixelHeight - mousePosition.y;
    5.    mousePosition = SceneView.currentDrawingSceneView.camera.ScreenToWorldPoint(mousePosition);
    6.    mousePosition.y = -mousePosition.y
    7. }
    8.  
     
  7. Dmsotir

    Dmsotir

    Joined:
    Feb 12, 2013
    Posts:
    43
    what kind of script does this go in? editor script? I cant seem to get it to work. its saying Event.curent.mousePosition is null...
     
  8. glitchers

    glitchers

    Joined:
    Apr 29, 2014
    Posts:
    64
  9. QuantumMagDev

    QuantumMagDev

    Joined:
    May 30, 2017
    Posts:
    2
    If anyone was looking for how to drag an object regardless of where you click on the screen via an Update function
    (i.e. for a player moving a ship on x and y across the screen) Feel free to use this code below simply have a variable to hold the oldMousePosition from Input.MousePosition in the ->class not in ->update;

    void Update()
    {
    Vector3 AmountToDrag = new Vector3();
    if (Input.mousePresent)
    {
    if (Input.GetMouseButtonDown(0))//Mouse Left Pressed
    {
    oldMousePos = Input.mousePosition;//Set to Current MousePos for Accurate Drag on next frame
    }
    else if (Input.GetMouseButton(0))//Mouse Left Held
    {
    AmountToDrag = new Vector3(Input.mousePosition.x - oldMousePos.x,
    Input.mousePosition.y - oldMousePos.y, 0);
    AmountToDrag *= 2;//This is to Raise/Lower Drag Sensitivity (2 = x2, 0.5 = x1/2)
    oldMousePos = Input.mousePosition;
    }
    else if (Input.GetMouseButtonUp(0))//Mouse Left Released
    {
    AmountToDrag = new Vector2(0, 0);
    }
    }

    Vector3 movement = new Vector3(AmountToDrag.x, AmountToDrag.y, 0);
    }


    Edit: Hope This Helps!
     
    Last edited: Mar 13, 2018
    CheekySparrow78 likes this.
  10. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    Just this will work perfectly. No need to flip or change anything in there.

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

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Thank you for saving my sanity. After a break from Unity I was bewildered how to do the simplest of things :p
     
    KimZigbang likes this.