Search Unity

Issue with in-game double clicking?

Discussion in 'Scripting' started by Rukas90, Mar 22, 2019.

  1. Rukas90

    Rukas90

    Joined:
    Sep 20, 2015
    Posts:
    169
    Hey everyone,
    I have this issue with double clicking in-game. I wrote this code that checks for input and returns the type of input it received.

    In editor it works just fine, but when I export the game it always returns double click type. Even if I click just once. Not sure what's causing this issue..

    Below is the mouse input script and other is how I use it in other scripts.

    Mouse Input script:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class MouseInput : MonoBehaviour
    5. {
    6.     #region Variables
    7.  
    8.     private enum ClickType      { None, Single, Double }
    9.  
    10.     private static ClickType    currentClick    = ClickType.None;
    11.     readonly float              clickdelay      = 0.25f;
    12.  
    13.     #endregion
    14.  
    15.     void OnEnable()
    16.     {
    17.         StartCoroutine(InputListener());
    18.     }
    19.     void OnDisable()
    20.     {
    21.         StopAllCoroutines();
    22.     }
    23.  
    24.     public static bool SingleMouseClick()
    25.     {
    26.         if (currentClick == ClickType.Single)
    27.         {
    28.             currentClick = ClickType.None;
    29.             return true;
    30.         }
    31.  
    32.         return false;
    33.     }
    34.     public static bool DoubleMouseClick()
    35.     {
    36.         if (currentClick == ClickType.Double)
    37.         {
    38.             currentClick = ClickType.None;
    39.             return true;
    40.         }
    41.  
    42.         return false;
    43.     }
    44.  
    45.     private IEnumerator InputListener()
    46.     {
    47.         while (enabled)
    48.         {
    49.             if (Input.GetMouseButtonDown(0))
    50.             { yield return ClickEvent(); }
    51.  
    52.             yield return null;
    53.         }
    54.     }
    55.     private IEnumerator ClickEvent()
    56.     {
    57.         if (EventSystem.current.IsPointerOverGameObject()) yield break;
    58.  
    59.         yield return new WaitForEndOfFrame();
    60.  
    61.         currentClick = ClickType.Single;
    62.         float count = 0f;
    63.         while (count < clickdelay)
    64.         {
    65.             if (Input.GetMouseButtonDown(0))
    66.             {
    67.                 currentClick = ClickType.Double;
    68.                 yield break;
    69.             }
    70.             count += Time.deltaTime;
    71.             yield return null;
    72.         }
    73.     }
    74. }
    Usage:
    Code (CSharp):
    1. if (MouseInput.SingleMouseClick())
    2. {
    3.      Debug.Log("Single click");
    4.      Select(true);
    5. }
    6. else if (MouseInput.DoubleMouseClick())
    7. {
    8.      Debug.Log("Double click");
    9.      Select(false);
    10. }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    So you're trying to detect when the user single- or double-clicks on the "background", not pointed at any object?

    Why is MouseInput line 59 waiting for end-of-frame instead of waiting for the next frame? I haven't tested, but my expectation would be that GetMouseButtonDown() will continue to return true until you actually get to the next frame, which means a single click could get the code through both checks. Not sure why it would be different in the editor than in the build, though.

    Also, I assume you realize that (if this code were working properly) every double-click would first be counted as a single-click, since there will be a window of time between the first and second click when currentClick has a value of Single.