RunActionOnMultiTap.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using UnityEngine.EventSystems;
  4. public class RunActionOnMultiTap : MonoBehaviour, IPointerClickHandler
  5. {
  6. [SerializeField]
  7. private UnityEvent m_OnTapped;
  8. [SerializeField]
  9. private float m_TimeThreshold = 1f;
  10. [SerializeField]
  11. private int m_TapThreshold = 3;
  12. private float m_Timer = 0f;
  13. private int m_Taps = 0;
  14. void Update()
  15. {
  16. if(m_Taps > 0)
  17. {
  18. m_Timer += Time.deltaTime;
  19. if(m_Timer > m_TimeThreshold)
  20. {
  21. m_Taps = 0;
  22. m_Timer = 0f;
  23. }
  24. else if(m_Taps >= m_TapThreshold)
  25. {
  26. m_OnTapped.Invoke();
  27. m_Taps = 0;
  28. m_Timer = 0f;
  29. }
  30. }
  31. }
  32. public void OnPointerClick(PointerEventData pointerEventData)
  33. {
  34. m_Taps++;
  35. }
  36. public void ToggleGameObject(GameObject go)
  37. {
  38. bool currentState = go.activeInHierarchy;
  39. go.SetActive(!currentState);
  40. }
  41. }