备份CatanBuilding瘦身独立工程
This commit is contained in:
72
Assets/Scripts/UIExtend/LongPressOrClickEventTrigger.cs
Normal file
72
Assets/Scripts/UIExtend/LongPressOrClickEventTrigger.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user