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. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Editor middle click detects Event.current.button as both 0 and 2

Discussion in 'Immediate Mode GUI (IMGUI)' started by spilat12, Oct 15, 2019.

  1. spilat12

    spilat12

    Joined:
    Feb 10, 2015
    Posts:
    32
    Hi, in the following code in the editor:

    Code (CSharp):
    1.         if (nameRect.Contains(Event.current.mousePosition))
    2.         {
    3.  
    4.             if (Event.current.button == 2)
    5.             {
    6.                 Debug.Log("pressed " + Event.current.button);
    7.             }
    8.             if (Event.current.button == 0)
    9.             {
    10.                 EditorGUIUtility.ShowObjectPicker<Object>(null, true, "", 3);
    11.                 Debug.Log("pressed " + Event.current.button);
    12.             }
    13. }
    It detects middle click first, but if I leave the mouse over the Rect, it then detects left click as well (which I never do, of course). I could not find any info on similar problems online, does anyone know what can be happening here? Thanks in advance!
     
  2. spilat12

    spilat12

    Joined:
    Feb 10, 2015
    Posts:
    32
    OK, so I found a solution accidentally. I do not understand why it works the way it does, but I'd be happy if someone explained. Here's the code that now works as intended:

    Code (CSharp):
    1.         Event e = Event.current;
    2.         if (e.button == 0 && e.isMouse)
    3.         {
    4.             Debug.Log("pressed " + e.button);
    5.         }
    6.         else if (e.button == 1)
    7.         {
    8.             EditorGUIUtility.ShowObjectPicker<Object>(null, true, "", 3);
    9.             Debug.Log("pressed " + e.button);
    10.         }
    First, it won't work properly if you don't use Event e = Event.current;
    Second, it won't work properly if you don't check e.isMouse
    Finally, it won't work properly if you first check e.button == 1 and then e.button == 0, like so:

    Code (CSharp):
    1.         Event e = Event.current;
    2.         if (e.button == 1 && e.isMouse)
    3.         {
    4.             Debug.Log("pressed " + e.button);
    5.         }
    6.         else if (e.button == 0)
    7.         {
    8.             EditorGUIUtility.ShowObjectPicker<Object>(null, true, "", 3);
    9.             Debug.Log("pressed " + e.button);
    10.         }
    I have no idea what's going on here...