Search Unity

Detect MouseDown for GUIBottons

Discussion in 'Scripting' started by zhutianlun810, Jul 1, 2020.

  1. zhutianlun810

    zhutianlun810

    Joined:
    Sep 17, 2017
    Posts:
    171
    Hello,
    I would like to detect MouseDown event for my GUIButtons in a scrolling view. I tried many times and till can't get it work.
    My code V1:
    Code (CSharp):
    1. GUILayout.BeginArea()
    2. EditorGUILayout.BeginScrollView();
    3. for (int i = 0; i < buttons.Length; i ++){
    4.     if (GUILayout.Button(buttons[i])){
    5.         if (currentEventType == EventType.MouseDown){
    6.             doing something...
    7.         }
    8.     }
    9. }
    10. EditorGUILayout.EndScrollView();
    11. GUILayout.EndArea();
    The code won't work because GUILayout.Button always detect EventType.MouseUp.

    My Code V2:
    Code (CSharp):
    1. GUILayout.BeginArea()
    2. EditorGUILayout.BeginScrollView();
    3. for (int i = 0; i < buttons.Length; i ++){
    4.     if (GUILayout.Button(buttons[i])){
    5.     }
    6.            if (currentEventType == EventType.MouseDown && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition)){
    7.             doing something...
    8.         }
    9. }
    10. EditorGUILayout.EndScrollView();
    11. GUILayout.EndArea();
    The code above won't work either, because GUILayoutUtility.GetLastRect() only works when
    Event.current.type == EventType.Repaint. And per OnGUI can only has one event.

    My Code V3:
    This is my last trial. I used a field called lastButtonRect outside the OnGUI and do something like:
    Code (CSharp):
    1.                 if (Event.current.type == EventType.Repaint)
    2.                 {
    3.                     lastButtonRect = GUILayoutUtility.GetLastRect();
    4.                 }
    5.              
    6.                 if (currentEventType == EventType.MouseDown)
    7.                 {
    8.                     Debug.Log("Mouse Down!!!!!!!!!!!!!!");
    9.                     Debug.Log(lastButtonRect);
    10.                     Debug.Log(Event.current.mousePosition);
    11.                 }
    I find that lastButtonRect is always the last button in the scroliing view. Therefore, It won't work.

    Is there any way to do this job?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    You could make an array for your buttons, and register the lastButtonRect for each of them.

    Or not use OnGUI, that's a horribly outdated interface.
     
    Suddoha likes this.
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    In principle, during the mousedown event, you'd have to examine the mouse position, and compare it to the coordinates of each of your buttons to determine which (if any) the mouse is currently inside.

    That means you'd need a list of the coordinates occupied by each button. Which you could probably create during OnGUI by calling GetLastRect after each and every one and saving the result to a variable somewhere, as Baste suggests. Alternately you could calculate via some other means where you think the buttons "should" be (at which point the OnGUI function would only be drawing pictures, and not actually involved in your input logic).

    Of course, if you were using UGUI instead, then you could just implement the IPointerDown interface.