最近在项目中做装备信息发送,鼠标左键和左ctrl键同时按下,发送选中的装备信息。一开始以为直接在Update函数中
Input.GetKeyDown(KeyCode.LeftControl)&&Input.GetMouseButtonDown(0),结果发现效果不是很好,毕竟两只手按下会有点时间差,导致不是很灵敏。
最后想了一个效果还可以的办法,类似于双击,但感觉好像又有点不同。请看代码:
if (Input.GetMouseButtonDown(0))
{
timer1 = Time.time;
if (timer1 != 0 && timer2 != 0)
{
if (Mathf.Abs(timer1 - timer2) < 0.5f)
{
timer1 = 0;
timer2 = 0;
Debug.Log("ok");
}
}
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
timer2 = Time.time;
if (timer1 != 0 && timer2 != 0)
{
if (Mathf.Abs(timer1 - timer2) < 0.5f)
{
timer1 = 0;
timer2 = 0;
Debug.Log("ok");
}
}
}
最后发现效果还不错。
UI上面实现双击:
实现接口IPointerClickHandler
eventData.clickCount==2即可监听双击事件