using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoubleClickTest : MonoBehaviour
{
private float doubleClickTimeLimit = 0.25f;
protected void Start()
{
StartCoroutine(InputListener());
}
// Update is called once per frame
private IEnumerator InputListener()
{
while (enabled)
{
if (Input.GetMouseButtonDown(0))
{
yield return ClickEvent();
}
yield return null;
}
}
private IEnumerator ClickEvent()
{
//暂停一帧所以不会记录相同鼠标点击事件
yield return new WaitForEndOfFrame();
float count = 0f;
while (count < doubleClickTimeLimit)
{
if (Input.GetMouseButtonDown(0))
{
DoubleClick();
yield break;
}
count += Time.deltaTime;// 根据两帧时间变化的增量计数器
yield return null; // 等待下一帧
}
SingleClick();
}
private void SingleClick()
{
Debug.Log("Single Click");
}
private void DoubleClick()
{
Debug.Log("Double Click");
}
}