- Home /
Issue with in-game double clicking?
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:
using System.Collections;
using UnityEngine.EventSystems;
public class MouseInput : MonoBehaviour
{
#region Variables
private enum ClickType { None, Single, Double }
private static ClickType currentClick = ClickType.None;
readonly float clickdelay = 0.25f;
#endregion
void OnEnable()
{
StartCoroutine(InputListener());
}
void OnDisable()
{
StopAllCoroutines();
}
public static bool SingleMouseClick()
{
if (currentClick == ClickType.Single)
{
currentClick = ClickType.None;
return true;
}
return false;
}
public static bool DoubleMouseClick()
{
if (currentClick == ClickType.Double)
{
currentClick = ClickType.None;
return true;
}
return false;
}
private IEnumerator InputListener()
{
while (enabled)
{
if (Input.GetMouseButtonDown(0))
{ yield return ClickEvent(); }
yield return null;
}
}
private IEnumerator ClickEvent()
{
if (EventSystem.current.IsPointerOverGameObject()) yield break;
yield return new WaitForEndOfFrame();
currentClick = ClickType.Single;
float count = 0f;
while (count < clickdelay)
{
if (Input.GetMouseButtonDown(0))
{
currentClick = ClickType.Double;
yield break;
}
count += Time.deltaTime;
yield return null;
}
}
}
Usage:
if (MouseInput.SingleMouseClick())
{
Debug.Log("Single click");
Select(true);
}
else if (MouseInput.DoubleMouseClick())
{
Debug.Log("Double click");
Select(false);
}
Answer by Ymrasu · Mar 22, 2019 at 11:59 PM
For some reason the problem is with yield return new WaitForEndOfFrame();
when building the game. I don't know if it ignores it or if it does wait till end of frame but it is still the current frame (only at the end of it). Replace it with yield return null
; it is very similar, but it waits till after the next Update(), which is guaranteed to be the next frame.
Answer by toddisarockstar · Mar 23, 2019 at 07:07 AM
dont be afraid of using timers in update sometimes it makes things a lot easier.
using UnityEngine;
using System.Collections;
public class waypoint : MonoBehaviour {
public int c = 0;
public int mouse = 0;
public float max = .15f;
public int mouseconfirmed = 0;
public float timer = 0;
void Update(){
mouse = -1;mouseconfirmed = -1;
if(timer>0){timer-=Time.deltaTime;
if (timer <=0) {timer = 0;mouseconfirmed = c;c = 0;}}
if(Input.GetMouseButtonDown(0)){c++;mouse = c;timer = max;}
if(mouse>0){print("down click: "+mouse);}
if(mouseconfirmed > 0) { print ("confirmed amount of double clicks" + mouseconfirmed); }
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Getting the text from UIInput 1 Answer
Projection to mouse position in isometric game 1 Answer
How to make shortcuts using two keys 1 Answer