Search Unity

Hurr? EventType.MouseUp not working on Left Clicks.

Discussion in 'Scripting' started by orionburcham, Aug 9, 2011.

  1. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Strange problem. Here's my C# code:

    Code (csharp):
    1.  
    2.  
    3. Event e = Event.current;
    4.                
    5. if(e.type == EventType.MouseUp)
    6. {
    7.     Debug.Log("Mouse Up!");
    8. }
    9.  
    10.  
    This works fine when I right click, and middle click, but not when I left click. A "MouseDown" version of this same code works fine for left clicks. any ideas? I am curfused.

    If anyone feels like it, can you try out the code on your end and see if you get similar results?

    Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Works fine here.

    --Eric
     
  3. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    hmm...are you on a pc? I'm on my MBP. maybe that's part of the issue.

    Oh! also, I'm using it in an editor script, so that could be something as well.
     
  4. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    crap, it works fine at runtime. looks like this is an editor problem. probably just have to work around it. :/ Thanks for testing it out.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, I'm on a Mac Pro. It works fine in the editor, using a standard OnGUI function anyway.

    --Eric
     
  6. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Ok, I'be been able to test some of these things to confirm, and it looks like it might be a strange isolated case when using an EditorWindow script with SceneView.OnSceneGUIDelegate. That's the only case where it seems to break, at least so far.

    thanks a lot for the help. I'm going to pursue a workaround for now.
     
    Last edited: Aug 9, 2011
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I will if you provide some code (too lazy to make an editor script just now...).

    --Eric
     
  8. mas

    mas

    Joined:
    Nov 14, 2010
    Posts:
    696
    Same problem here .. did not check on Mac but on Win7 64.

    Put code in an editor script ( custom editor script )
    and

    click holding the shift key

    Code (csharp):
    1.  
    2.  
    3.  void StartSelection()
    4.     {
    5.         print("start selection");
    6.     }
    7.  
    8.     void EndSelection()
    9.     {
    10.         print("end selection");
    11.     }
    12.  
    13.  
    14.     void HandleEvents()
    15.     {
    16.         Event e = Event.current;
    17.         if (e.type == EventType.MouseDown)
    18.         {
    19.             if (e.shift)
    20.             {
    21.                 StartSelection();
    22.                 e.Use();
    23.             }
    24.         }
    25.         if (e.type == EventType.MouseUp)
    26.         {
    27.             if (e.shift)
    28.             {
    29.                 EndSelection();
    30.                 e.Use();
    31.             }
    32.         }
    33.     }
    34.  
    35.   void OnSceneGUI()
    36.   {
    37.       HandleEvents();
    38.   }
    39.  
    40.  
    41.  

    I get a 'start selection' but no 'end selection' when using left mouse
    I get a 'start selection' and 'end selection' when using right or middle mouse

    even more strange ...

    After one good click (right or middle) , I get one good click with the left mouse button
    before it falls back to the failing behaviour.

    Right button to multi - select control points in my spline for now......

    UPDATE
    -----------

    More people experience the same problem
    http://groups.google.com/group/unity3d-developers/browse_thread/thread/78169691889b8fdd?pli=1

    Hinted is to set some editor setting to .. none , so default move/rotate/scale tool does not capture this mouseUp event...

    anyone knows what setting this would be?
     
    Last edited: Aug 10, 2011
  9. mas

    mas

    Joined:
    Nov 14, 2010
    Posts:
    696
    Found the setting that should do the trick, according to the suggestion in my previous posted link,
    but it did not work. :(

    setting
    Code (csharp):
    1.  
    2. Tools.current = Tool.None;
    3.  
    Does not stop the 'mysterious' object from consuming my MouseUp event.
     
  10. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,767
    Did anyone find a fix for this, I to can get no mouseUp event in my OnSceneGUI(). Is there a workaround for this?
     
  11. MADmarine

    MADmarine

    Joined:
    Aug 31, 2010
    Posts:
    627
  12. Wenceslao

    Wenceslao

    Joined:
    Feb 7, 2009
    Posts:
    142
    Thank you so much for finding that MADmarine (Thanks to dagbud for the linked post!) It solved the problem I had been struggling with for a couple of nights. Now my tool allows me to populate waypoints in the scene quickly and efficiently and the editor isn't trying to use my input at the same time.
     
  13. vladibo

    vladibo

    Joined:
    Jan 29, 2013
    Posts:
    38
    Code (csharp):
    1.  
    2. void OnSceneGUI(){
    3.        Event e = Event.current;
    4.  
    5.         var controlID = GUIUtility.GetControlID(FocusType.Passive);
    6.         var eventType = e.GetTypeForControl(controlID);
    7.         if (eventType == EventType.MouseUp)
    8.         {
    9.             Debug.Log("Mouse Up!");
    10.             GUIUtility.hotControl = controlID;
    11.             e.Use();
    12.         }
    13.         else if (eventType == EventType.MouseDrag)
    14.         {
    15.             Debug.Log("Mouse Drag!");
    16.             e.Use();
    17.         }
    18.         else if (eventType == EventType.MouseDown)
    19.         {
    20.             Debug.Log("Mouse Down!");
    21.             GUIUtility.hotControl = 0;
    22.             e.Use();
    23.         }
    24.  
    25.  
     
  14. ocimum

    ocimum

    Joined:
    Apr 19, 2015
    Posts:
    12
    This tutorial made my day!
    It explains how to get the different Eventtypes work without losing the focus after clicking on the scene!
     
  15. Amitloaf

    Amitloaf

    Joined:
    Jan 30, 2012
    Posts:
    97
    For some reason (which I don't fully understand) the only thing that helped me was adding this line in the beginning of the OnSceneGui method:
    Code (CSharp):
    1. HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive))
     
  16. VLukianenko

    VLukianenko

    Joined:
    Mar 27, 2017
    Posts:
    30
    Adding the line posted by Amitloaf above indeed helped! Now I can register mouse up in my OnSceneGUI! Thanks a lot!
     
    Quatum1000 and Amitloaf like this.
  17. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    For me it works in Scene View on 2019.1.19f

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. /// <summary>
    4. ///
    5. /// </summary>
    6. [ExecuteInEditMode]
    7. public class MouseTest: MonoBehaviour {
    8.  
    9.     public bool m_down = false;
    10.     public bool m_up = false;
    11.  
    12.     private void OnEnable() {
    13.         if (!Application.isEditor) {
    14.             Destroy(this);
    15.         }
    16.         SceneView.duringSceneGui += OnScene;
    17.         m_down = false;
    18.     }
    19.  
    20.     void OnScene(SceneView scene) {
    21.  
    22.         HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
    23.         Event e = Event.current;
    24.  
    25.         if (e.type == EventType.MouseDown) {
    26.             Debug.Log("down");
    27.             m_down = true;
    28.         }
    29.  
    30.         if (m_down) {
    31.             if (e.type == EventType.MouseUp) {
    32.                 Debug.Log("up");
    33.                 m_down = false;
    34.             }
    35.         }
    36.  
    37.        if(m_down) {
    38.            // do anything while mouse down
    39.        }
    40.  
    41.   }
    42. }
     
    jeremedia and Amitloaf like this.
  18. Mariusz-Born7

    Mariusz-Born7

    Joined:
    Sep 4, 2015
    Posts:
    40
    If you don't want to interfere with editor events and be able to select objects and just be notified about MouseUp event, you can check it this way:

    Code (CSharp):
    1.  
    2. Event current = Event.current;
    3. if (current.type == EventType.MouseUp && current.button == 0)
    4. {
    5. Debug.Log("LMB up");
    6. }
    7. else if (current.type == EventType.Used && current.button == 0 && current.pointerType == PointerType.Mouse)
    8. {
    9. Debug.Log("LMB up used");
    10. }