Search Unity

Double click in GUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by besuser, Dec 7, 2007.

  1. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    I'm currently trying to create a window to display a directory and allow the user to select a location to save a file. There doesn't seem to be a GUI control for this so I'm using a series of labels and the scrollview to create a list box. On top of each label, I've placed a toggle button so I can capture a mouse click over a label and highlight that label. Now I'm trying to capture a double click on a toggle button. Is there a way to do capture double clicks? or does anyone have a better solution for a directory dialog box. Thanks all.
     
  2. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    heres some converted code that i use in my project... i've removed some parts to make it more readable... but may have broken it in the process ;)

    Code (csharp):
    1.  void OnMouseDown()
    2.     {
    3.         if (intClickCount == 0)
    4.         {
    5.             intClickCount++;
    6.             StartCoroutine("mCheckMouseClick");
    7.         }
    8.         else
    9.         {
    10.             intClickCount++;
    11.         }
    12.     }
    13.     IEnumerator mCheckMouseClick()
    14.     {
    15.         if (intClickCount == 1)
    16.         {
    17.             yield return new WaitForSeconds(0.4f); //This sets the delay for the double click validation
    18.             if (intClickCount > 1)
    19.             {
    20.                 //Debug.Log("Double Clicked");
    21.                 intClickCount = 0;
    22. //Do double click stuff here
    23.             }
    24.             else
    25.             {
    26.                 //Debug.Log("Single Clicked");
    27.                 intClickCount = 0;
    28.             }
    29.         }
    30.     }
     
  3. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    What we did in the terrain inspector was to use Event.current.clickCount.

    Something like this:

    Code (csharp):
    1.  
    2. if (GUI.Button (Rect (10,10,100,20), "DoubleClick me"))  Event.current.clickCount == 2) {
    3.    // This button was doubleclicked within the time specified in the user's system prefs for  double clicking
    4. }
    5.  
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    I dont know if this is of use -it may be to beginners-but I went over Shauns and tidied it up. Works fine in 1.6.2. Thanks Shaun, very helpful. This is a javascript:
    Code (csharp):
    1. private var intClickCount=0;
    2. function OnMouseDown()
    3.     {
    4.         if (intClickCount == 0)
    5.         {
    6.             intClickCount++;
    7.             StartCoroutine("mCheckMouseClick");
    8.         }
    9.         else
    10.         {
    11.             intClickCount++;
    12.         }
    13.     }
    14.     function mCheckMouseClick()
    15.     {
    16.         if (intClickCount == 1)
    17.         {
    18.             yield WaitForSeconds(0.4); //This sets the delay for the double click validation
    19.             if (intClickCount > 1)
    20.             {
    21.                 Debug.Log("Double Clicked");
    22.                 intClickCount = 0;
    23. //Do double click stuff here
    24.             }
    25.             else
    26.             {
    27.                 Debug.Log("Single Clicked");
    28.                 intClickCount = 0;
    29.             }
    30.         }
    31.     }
    AC
     
  5. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    Wow!! Thank you Unity gurus!! I will try all these out. I'm sure one of them will fit my needs perfectly.
     
  6. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    I tried using Event.current.clickCount and it works great when I publish for the mac, but it doesn't seem to work when publishing for the pc. Could this be a bug?

    As for the OnMouseDown solution, will it work with the built-in GUI system? From what I've read in the docs, it only works for GUIElements. If so, I guess I'd need to attach the script to a GUIText or GUITexture object.

    Anyhow, just an update to people know if these worked out.
     
  7. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    This was why I wrote my own click handler routine - I tried a few times but couldnt get clickcount working - i thought it was just me :)
     
  8. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    In these cases, please file bugs. I'll look into making sure it's in the next release.