I have come up with this double click versus single click (or touch) algorithm but it is not perfect.
It is detecting single clicks and double clicks OK except that it can’t detect a double click with out first detecting a single click, i.e. I am seeing two events for a double click rather than just one.
And it is a bitch to debug because any break points in this code obviously stuff it up.
And Debug.Log(“”) statement in the wrong place slow it down terribly.
Has anyone tried this previously and had success and would be prepared to share?
Or perhaps improve on my algorithm below.
There is probably a far simpler way to do this but it escapes me at present.
protected void CheckForMouseClickOrTouch()
{
if (m_fDoubleClickTimer >= 1.5f)
{
m_fDoubleClickTimer = -1.0f;
//Debug.Log("m_fDoubleClickTimer = -1.0f;");
}
else if (m_fDoubleClickTimer >= 0.0f)
{
m_fDoubleClickTimer += Time.deltaTime;
//Debug.Log("m_fDoubleClickTimer += Time.deltaTime;");
}
if (Input.GetButtonDown("Fire1"))
{
Vector3 vectTouchPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
Collider2D collider = GetComponent<Collider2D>();
bool bHit = collider.OverlapPoint(vectTouchPos);
if (bHit)
{
if ((m_fDoubleClickTimer >= 0.0f) && (m_fDoubleClickTimer <= 0.5f))
{
m_bDoubleClicked = true;
m_bClicked = false;
//Debug.Log("m_bDoubleClicked = true");
}
else
{
m_fDoubleClickTimer = 0.0f;
m_bDoubleClicked = false;
m_bClicked = true;
//Debug.Log("m_bClicked = true");
}
}
}
}
//=====================================================================================================================================================
// FUNCTIONS: WasClicked()
//=====================================================================================================================================================
// This function detects both mouse clicks and touches within your game object.
// It requires that your game object has a collider component added to it.
// It is overridable should you need to vary the behavior in any derived classes.
//=====================================================================================================================================================
protected virtual bool WasClicked()
{
bool bHit = m_bClicked;
m_bClicked = false;
return bHit;
}
//=====================================================================================================================================================
// FUNCTIONS: WasDoubleClicked()
//=====================================================================================================================================================
// This function detects both mouse clicks and touches within your game object.
// It requires that your game object has a collider component added to it.
// It is overridable should you need to vary the behavior in any derived classes.
//=====================================================================================================================================================
protected virtual bool WasDoubleClicked()
{
bool bHit = m_bDoubleClicked;
m_bDoubleClicked = false;
return bHit;
}
protected override void Update()
{
CheckForMouseClickOrTouch();
if (WasDoubleClicked())
{
SendMessageDirect(m_gameobjBingoManager,BingoMessageIDEnum.MSGID_TUMBLER_DOUBLE_CLICKED);
}
else if (WasClicked())
{
SendMessageDirect(m_gameobjBingoManager,BingoMessageIDEnum.MSGID_TUMBLER_CLICKED);
}
}