72 lines
1.8 KiB
C#
72 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class LongPressOrClickEventTrigger : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler,
|
|
IPointerClickHandler
|
|
{
|
|
public float durationThreshold = 1.0f;
|
|
|
|
public UnityEvent onLongPress = new UnityEvent();
|
|
public UnityEvent onClick = new UnityEvent();
|
|
public UnityEvent onDown = new UnityEvent();
|
|
public UnityEvent onUp = new UnityEvent();
|
|
|
|
private bool isPointerDown = false;
|
|
private bool longPressTriggered = false;
|
|
private float timePressStarted;
|
|
|
|
private void Update()
|
|
{
|
|
if (isPointerDown && !longPressTriggered)
|
|
{
|
|
if (Time.time - timePressStarted > 0.2f)
|
|
{
|
|
onLongPress.Invoke();
|
|
longPressTriggered = true;
|
|
}
|
|
|
|
// if (Time.time - timePressStarted > 0.2)
|
|
// {
|
|
// longPressTriggered = true;
|
|
// onLongPress.Invoke();
|
|
// }
|
|
}
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
timePressStarted = Time.time;
|
|
isPointerDown = true;
|
|
longPressTriggered = false;
|
|
onDown.Invoke();
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
isPointerDown = false;
|
|
onUp.Invoke();
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
isPointerDown = false;
|
|
// onUp.Invoke();
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
if (!longPressTriggered)
|
|
{
|
|
onClick.Invoke();
|
|
}
|
|
}
|
|
protected override void OnDisable()
|
|
{
|
|
base.OnDisable();
|
|
isPointerDown = false;
|
|
longPressTriggered = false;
|
|
}
|
|
} |