unity 实现双击物体让其隐藏,单击物体让其显示

unity 实现双击物体让其隐藏,单击物体让其显示
private float tapThreshold = 0.25f;
private float tapTimer = 0.0f;
private bool tap = false;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (Time.time < this.tapTimer + this.tapThreshold)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 创建射线从相机指向鼠标点击位置
if (Physics.Raycast(ray, out hit)) // 如果射线击中了物体
{
GameObject clickedObject = hit.transform.gameObject; // 获取被点击的物体
if (clickedObject == this.gameObject) // 如果是当前物体被点击
{
Debug.Log(“双击”);
this.tap = false;
return;
}
}

        }
        this.tap = true;
        this.tapTimer = Time.time;
    }
    if (this.tap == true && Time.time > this.tapTimer + this.tapThreshold)
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 创建射线从相机指向鼠标点击位置
        if (Physics.Raycast(ray, out hit)) // 如果射线击中了物体
        {
            GameObject clickedObject = hit.transform.gameObject; // 获取被点击的物体
            if (clickedObject == this.gameObject) // 如果是当前物体被点击
            {
                this.tap = false;
                Debug.Log("单击");
                return;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44919646/article/details/132875917
今日推荐