using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoubleClickDemo : MonoBehaviour {
private Ray ray;
private RaycastHit hit;
private float time;
//private void OnGUI()
//{
// DoubleClickActionOne();
//}
//private void Update()
//{
// DoubleClickActionTwo();
//}
//方法一
private void DoubleClickActionOne()
{
if (Event.current.isMouse && Event.current.type == EventType.MouseDown && Event.current.clickCount == 2)
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 50))
{
print("double click");
}
}
}
//方法二
private void DoubleClickActionTwo()
{
if (IsDoubleClick(0.2f, ref time, 0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 50))
{
print("double click");
}
}
}
private bool IsDoubleClick(float offsetTime,ref float tim,int mouseIndex)
{
if (Input.GetMouseButtonDown(mouseIndex))
{
return IsDoubleClickTimer(offsetTime, ref tim);
}
return false;
}
private bool IsDoubleClickTimer(float offsetTime, ref float tim)
{
if (Time.time - tim < offsetTime)
return true;
else
{
tim = Time.time;
return false;
}
}
}
unity鼠标双击
最新推荐文章于 2024-02-02 11:08:23 发布