Guest User

Untitled

a guest
Jun 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class TouchController : MonoBehaviour
  6. {
  7.  
  8.  
  9. public delegate void TouchEventHandler(Vector2 swipe);
  10.  
  11. public static event TouchEventHandler SwipeEvent;
  12. public static event TouchEventHandler SwipeEndEvent;
  13.  
  14. Vector2 m_touchMovement;
  15.  
  16. float m_minSwipeDistance = 25;
  17. float m_newSwipeDistance = 0;
  18.  
  19. void OnSwipe()
  20. {
  21. if (SwipeEvent != null)
  22. {
  23. SwipeEvent(m_touchMovement);
  24. }
  25. }
  26.  
  27. void OnSwipeEnd()
  28. {
  29. if (SwipeEndEvent != null)
  30. {
  31. SwipeEndEvent(m_touchMovement);
  32. }
  33. }
  34.  
  35. // public Text m_diagnosticText1;
  36. // public Text m_diagnosticText2;
  37.  
  38. public bool m_useDiagnostic = false;
  39.  
  40. // Use this for initialization
  41. void Start()
  42. {
  43. Diagnostic("", "");
  44. }
  45.  
  46.  
  47. void Diagnostic(string text1, string text2)
  48. {
  49. //if (m_diagnosticText1 && m_diagnosticText2)
  50. //{
  51. // m_diagnosticText1.gameObject.SetActive(m_useDiagnostic);
  52. // m_diagnosticText2.gameObject.SetActive(m_useDiagnostic);
  53.  
  54. //}
  55. //if (m_diagnosticText1 != null && m_diagnosticText2 != null)
  56. //{
  57. // m_diagnosticText1.text = text1;
  58. // m_diagnosticText2.text = text2;
  59. //}
  60.  
  61. }
  62.  
  63. string SwipeDiagnostic(Vector2 swipeMovement)
  64. {
  65. string direction = "";
  66.  
  67. // horizontal
  68. if (Mathf.Abs(swipeMovement.x) > Mathf.Abs(swipeMovement.y))
  69. {
  70. direction = (swipeMovement.x >= 0) ? "right" : "left";
  71. }
  72. // vertical
  73. else
  74. {
  75. direction = (swipeMovement.y >= 0) ? "up" : "down";
  76. }
  77. return direction;
  78. }
  79.  
  80.  
  81. // Update is called once per frame
  82. void Update()
  83. {
  84. if (Input.touchCount > 0)
  85. {
  86. Touch touch = Input.touches[0];
  87.  
  88. if (touch.phase == TouchPhase.Began)
  89. {
  90. m_touchMovement = Vector2.zero;
  91. Diagnostic("", "");
  92.  
  93. }
  94. else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
  95. {
  96.  
  97. m_touchMovement += touch.deltaPosition;
  98. // m_newSwipeDistance = m_touchMovement.magnitude;
  99.  
  100. if (m_touchMovement.magnitude > m_minSwipeDistance)
  101. {
  102. OnSwipe();
  103. Diagnostic("Swipe detected", m_touchMovement.ToString() + " " + SwipeDiagnostic(m_touchMovement));
  104. }
  105. else if(m_touchMovement.magnitude < m_minSwipeDistance/6)
  106. {
  107. m_touchMovement = Vector2.zero;
  108. }
  109. }
  110. else if (touch.phase == TouchPhase.Ended)
  111. {
  112. OnSwipeEnd();
  113. }
  114. }
  115. }
  116.  
  117. }
Add Comment
Please, Sign In to add comment