Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Double-click actions.

Discussion in 'Scripting' started by Jessy, Jul 2, 2007.

  1. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Hey!

    I am trying to figure how to utilize double-clicking to get things to happen. I want single-clicking to do one thing, double-clicking to do another, and double-click-and-holding to do yet another. I assume this will involve creative scripting using time differences, but I am a total novice at this. Thanks for any advice!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    This might get you started:

    Code (csharp):
    1. var doubleclickTime : float = .5;
    2. private var clickTime : float = -999;
    3. private var doubleclicked = false;
    4.  
    5. function OnMouseDown () {
    6.     if (Time.time < clickTime + doubleclickTime  !doubleclicked) {
    7.         doubleclicked = true;
    8.     }
    9.     else {
    10.         print ("Click!");
    11.         doubleclicked = false;
    12.     }
    13. }
    14.  
    15. function OnMouseUp () {
    16.     clickTime = Time.time;
    17.     if (doubleclicked) {
    18.         print ("Doubleclick!");
    19.     }
    20. }
    You might want different logic depending on what sort of behavior you're after exactly. I think that code mimics clicking behavior in the Finder, whereas a website works differently (clicks register on mouseup instead). There's also an OnMouseDrag function that might be useful, though I don't know if you're using OnMouse at all.

    --Eric