Search Unity

Unity Editor mouse clicks in 3D Viewport

Discussion in 'Scripting' started by ReiAyanami, Aug 5, 2012.

  1. ReiAyanami

    ReiAyanami

    Joined:
    Aug 6, 2010
    Posts:
    53
    Hello all!

    I started working on a custom editor for a project, and this editor requires to react on mouse clicks in the 3D editor viewport.

    I was able to use a CustomEditor and overwriting the EventType.layout with setting the selection to an empty array and HandleUtility.AddDefaultControl. However, this allows me only to get one mouse click, and not check if the mouse was released again. Plus, of course, this is a really unclean "hacky" solution, which I would like to avoid.

    Basically I just want to poll the mouse position every frame, check if the mouse button (and which one) was pressed, and from there on I think I will be fine.

    Thanks in advance,
    ReiAyanami
     
  2. Lukas_GMC

    Lukas_GMC

    Joined:
    Aug 7, 2012
    Posts:
    40
    I was able to do it without using AddDefaultControl. This is in my customEditor script for my polygon create tool. Seems to work pretty well for me.
    Code (csharp):
    1.  
    2. public void OnSceneGUI()
    3. {
    4.   Event e = Event.current;
    5.  
    6.   //Check the event type and make sure it's left click.
    7.   if ((e.type == EventType.MouseDrag || e.type == EventType.MouseDown)  e.button == 0)
    8.   {
    9.      /* Do stuff
    10.         * * * *
    11.       * */
    12.      e.Use();  //Eat the event so it doesn't propagate through the editor.
    13.   }
    14. }
     
    Last edited: Aug 9, 2012
    FlightOfOne likes this.
  3. ReiAyanami

    ReiAyanami

    Joined:
    Aug 6, 2010
    Posts:
    53
    Thanks for the answers.

    I was finally able to figure out a good solution with your help and this thread.

    Thanks again!
     
  4. higekun

    higekun

    Joined:
    Nov 15, 2012
    Posts:
    2
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    For people who need to have a SceneView mouse position outside a class that has OnSceneGUI(), for example, a menu invoke, plugin, or whatever...

    Code (csharp):
    1.  
    2. /// <summary>
    3. /// This is used to find the mouse position when it's over a SceneView.
    4. /// Used by tools that are menu invoked.
    5. /// </summary>
    6. [InitializeOnLoad]
    7. public class MouseHelper : Editor
    8. {
    9.     private static Vector2 position;
    10.  
    11.     public static Vector2 Position
    12.     {
    13.         get { return position; }
    14.     }
    15.  
    16.     static MouseHelper()
    17.     {
    18.         SceneView.onSceneGUIDelegate += UpdateView;
    19.     }
    20.  
    21.     private static void UpdateView(SceneView sceneView)
    22.     {
    23.         if (Event.current != null)
    24.             position = new Vector2(Event.current.mousePosition.x + sceneView.position.x, Event.current.mousePosition.y + sceneView.position.y);
    25.     }
    26. }
    27.  
     
    tkayaist, jmortiger, olix4242 and 2 others like this.