CultivationOfBrewing-2/Assets/Scripts/Project/Objects/Other/TapGesture.cs

79 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class TapGesture : MonoBehaviour
{
private bool isClick = false;
private float lastClick = 0;
private int clickCount = 0;
private float timeCount = 0.12f;
public UnityAction onOneClick;
public UnityAction onDoubleClick;
public LayerMask lm;
private void Start()
{
}
public virtual void Update()
{
if (EventSystem.current.IsPointerOverGameObject()) return;
if (Input.GetMouseButtonDown(0))
{
timeCount = 0.12f;
if (Time.realtimeSinceStartup - lastClick < 0.18f)
{
clickCount = 2;
}
else
{
clickCount = 1;
}
}
if (Input.GetMouseButtonUp(0))
{
isClick = false;
lastClick = Time.realtimeSinceStartup;
}
if (!isClick)
{
timeCount -= Time.deltaTime;
if (timeCount <= 0)
{
if (clickCount == 2)
{
onDoubleClick?.Invoke();
}
else if (clickCount == 1)
{
onOneClick?.Invoke();
}
isClick = true;
clickCount = 0;
}
}
}
public void SetLayerMask(LayerMask _lm)
{
lm = _lm;
}
public void OnRay(ref GameObject _target)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out var hitInfo, 300, lm))
{
_target = hitInfo.collider.gameObject;
//Debug.Log(_target.name);
}
else
{
//Debug.Log("Physics");
}
Debug.DrawLine(ray.origin, hitInfo.point, Color.red, 1);
}
}