-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongPressEventTrigger.cs
45 lines (34 loc) · 1.34 KB
/
LongPressEventTrigger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
namespace System
{
public class LongPressEventTrigger : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
[Tooltip("How often events should be triggered when the button is pressed (The lower the value, the more often; the higher the value, the less often)")]
public float tact = 0.062f;
[Space(10)]
public UnityEvent onLongPress = new UnityEvent();
private bool _isPointerDown;
protected override void OnEnable()
{
base.OnEnable();
StartCoroutine(EventUpdate());
}
protected override void OnDisable()
{
base.OnDisable();
StopCoroutine(EventUpdate());
}
private IEnumerator EventUpdate()
{
if (_isPointerDown) onLongPress.Invoke();
yield return new WaitForSeconds(tact);
StartCoroutine(EventUpdate());
}
public void OnPointerDown(PointerEventData eventData) => _isPointerDown = true;
public void OnPointerUp(PointerEventData eventData) => _isPointerDown = false;
public void OnPointerExit(PointerEventData eventData) => _isPointerDown = false;
}
}