140 lines
3.7 KiB
C#
140 lines
3.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class TapGesture : MonoBehaviour
|
|
{
|
|
private bool isClick = false;
|
|
private float lastClick = 0;
|
|
public int clickCount = 0;
|
|
public float timeCount = 0.1f;
|
|
public EventSystem eventsystem;
|
|
public GraphicRaycaster RaycastInCanvas;
|
|
public UnityEvent onOneClick;
|
|
public UnityEvent onDoubleClick;
|
|
|
|
[SerializeField]
|
|
private Camera _camera;
|
|
public bool debug;
|
|
private RaycastHit _result = new RaycastHit();
|
|
private Ray _ray;
|
|
public LayerMask _layerMask = Physics.DefaultRaycastLayers;
|
|
|
|
private void Update()
|
|
{
|
|
if (CheckGuiRaycastObjects())
|
|
{
|
|
return;
|
|
}
|
|
//_ray = Raycast(Input.mousePosition, out _result);
|
|
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
timeCount = 0.1f;
|
|
if (Time.realtimeSinceStartup - lastClick < 0.2f)
|
|
{
|
|
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();
|
|
Debug.Log("2");
|
|
}
|
|
else if (clickCount == 1)
|
|
{
|
|
onOneClick?.Invoke();
|
|
Debug.Log("1");
|
|
}
|
|
isClick = true;
|
|
clickCount = 0;
|
|
|
|
}
|
|
}
|
|
}
|
|
public void SetLayerMask(int layerMask = Physics.DefaultRaycastLayers)
|
|
{
|
|
_layerMask = layerMask;
|
|
}
|
|
public Ray GetRay()
|
|
{
|
|
return _ray;
|
|
}
|
|
public RaycastHit HitTest()
|
|
{
|
|
return _result;
|
|
}
|
|
public RaycastHit HitTest(Vector3 position, bool worldPosition)
|
|
{
|
|
RaycastHit result = new RaycastHit();
|
|
if (worldPosition == true) position = _camera.WorldToScreenPoint(position);
|
|
Raycast(position, out result);
|
|
return result;
|
|
}
|
|
|
|
public Ray Raycast(Vector3 pos, out RaycastHit hitInfo)
|
|
{
|
|
|
|
if (_camera == null) _camera = Camera.main;
|
|
Ray ray = _camera.ScreenPointToRay(pos);
|
|
// regular 3D raycast
|
|
bool hit = Physics.Raycast(ray, out hitInfo, Mathf.Infinity, _layerMask);
|
|
//#if UNITY_EDITOR
|
|
// vizualise ray
|
|
//lineRenderer.SetPosition(0, ray.origin+new Vector3(0.0f,-0.3f));
|
|
if (debug)
|
|
{
|
|
if (hit)
|
|
{
|
|
Vector3 hitPos = hitInfo.point;
|
|
|
|
Debug.DrawLine(ray.origin, hitPos, Color.green, 0.5f);
|
|
//lineRenderer.SetPosition(1, hitPos);
|
|
}
|
|
else
|
|
{
|
|
//Debug.Log("Raycase " +debug+" -> "+ ray);
|
|
Debug.DrawLine(ray.origin, ray.origin + ray.direction * 9999.0f, Color.red, 0.5f);
|
|
//lineRenderer.SetPosition(1, ray.origin + ray.direction * 50);
|
|
}
|
|
}
|
|
//#endif
|
|
return ray;
|
|
}
|
|
|
|
/// <summary>
|
|
/// UI 防穿透
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool CheckGuiRaycastObjects()
|
|
{
|
|
PointerEventData eventData = new PointerEventData(eventsystem);
|
|
eventData.pressPosition = Input.mousePosition;
|
|
eventData.position = Input.mousePosition;
|
|
List<RaycastResult> list = new List<RaycastResult>();
|
|
RaycastInCanvas.Raycast(eventData, list);
|
|
return list.Count > 0;
|
|
}
|
|
}
|