Created
March 3, 2020 10:01
-
-
Save gie3d/47d1cb9eec69ea210dc7934f6215d5e0 to your computer and use it in GitHub Desktop.
Unity Long Press script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
using UnityEngine.Events; | |
using System; | |
public class LongPress : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler | |
{ | |
[Tooltip("Time before the long press triggered")] | |
public float holdTime = 0.001f; | |
[Tooltip("long press event trigger interval")] | |
public float intervalTime = 0.001f; | |
public UnityEvent onClick = new UnityEvent(); | |
public UnityEvent onLongPress = new UnityEvent(); | |
public void OnPointerDown(PointerEventData eventData) | |
{ | |
onClick.Invoke(); | |
InvokeRepeating("OnLongPress", holdTime, intervalTime); | |
} | |
public void OnPointerUp(PointerEventData eventData) | |
{ | |
//Debug.Log("Stop Long Pressing"); | |
CancelInvoke("OnLongPress"); | |
} | |
public void OnPointerExit(PointerEventData eventData) | |
{ | |
//Debug.Log("Stop Long Pressing"); | |
CancelInvoke("OnLongPress"); | |
} | |
private void OnLongPress() | |
{ | |
try | |
{ | |
//Debug.Log("Long Press is ongoing"); | |
onLongPress.Invoke(); | |
} | |
catch (Exception ex) | |
{ | |
Debug.Log(ex.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment