using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
namespace GameFramework
{
public class LongPressOrClickEventTrigger : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerClickHandler
{
public float durationThreshold = 1.0f;
public UnityEvent onLongPress = new UnityEvent();
public UnityEvent onClick = new UnityEvent();
private bool isPointerDown = false;
private bool longPressTriggered = false;
private float timePressStarted;
private void Update()
{
if (isPointerDown && !longPressTriggered)
{
if (Time.time - timePressStarted > durationThreshold)
{
longPressTriggered = true;
onLongPress.Invoke();
}
}
}
public void OnPointerDown(PointerEventData eventData)
{
timePressStarted = Time.time;
isPointerDown = true;
longPressTriggered = false;
}
public void OnPointerUp(PointerEventData eventData)
{
isPointerDown = false;
}
public void OnPointerExit(PointerEventData eventData)
{
isPointerDown = false;
}
public void OnPointerClick(PointerEventData eventData)
{
if (!longPressTriggered)
{
onClick.Invoke();
}
}
}
}
使用:
var pressSrc = Obj_ClickButton.gameObject.AddComponent<LongPressOrClickEventTrigger>();
pressSrc.onLongPress.AddListener(delegate ()
{
//长按方法
});
pressSrc.onClick.AddListener(delegate ()
{
//点击方法
});