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. Dismiss Notice

Resolved Stop/reset mouse event after a certain click count

Discussion in 'UI Toolkit' started by mbiggs2334, May 9, 2023.

  1. mbiggs2334

    mbiggs2334

    Joined:
    Mar 31, 2022
    Posts:
    32
    Hello. I'm wanting to perform an action after a double click. That works fine, but if you double click rapidly it continues along the first mouse event and the click count continues counting up beyond two. Is there any way to reset the click count or change the event double click time?

    Edit: Forgot to mention I'm registering a MouseDownEvent callback.
     
    Last edited: May 9, 2023
  2. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    I don't think you can reset the click count. I'm not sure if you can change the double-click time; I believe it uses the OS settings, but I'm not sure.

    Maybe you could partially solve this with code. Here's a function that will return a periodical clickCount (so 3 is 1, 4 is 2, and so on):

    Code (CSharp):
    1.  
    2.         int GetPeriodicalClickCount(int clickCount, int maxCount = 2)
    3.         {
    4.             // For the next operation to work, the count needs to start at zero.
    5.             int zeroBasedCount = clickCount - 1;
    6.             // The modulo operator %, returns the reminder of dividing the left number by the right number.
    7.             // For example, 0/2 reminder's is 0, 1/2 is 1, 2/2 is 0, 3/2 is 1, and so on.
    8.             int periodicalZeroBasedCount = (zeroBasedCount % maxCount);
    9.  
    10.             // Finally, we add one to transform the 0s and 1s to 1s and 2s.
    11.             return periodicalZeroBasedCount + 1;
    12.         }
    13.  
    14.         // You can use it like this:
    15.         void OnMouseDown(MouseDownEvent e)
    16.         {
    17.             int doubleClickCount = GetPeriodicalClickCount(e.clickCount);
    18.             // Pass another number as a second parameter if you care about triple-click counts, for example:
    19.             int tripleClickCount = GetPeriodicalClickCount(e.clickCount, 3);
    20.         }
    21.  
     
  3. mbiggs2334

    mbiggs2334

    Joined:
    Mar 31, 2022
    Posts:
    32
    Forgot about this post, thank you Oscar for the advice.

    I ended up solving my problem by trading this out
    if (event.clickCount == 2)

    for
    if (event.clickCount % 2 == 0)
     
    oscarAbraham likes this.