Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class TouchController : MonoBehaviour
- {
- public delegate void TouchEventHandler(Vector2 swipe);
- public static event TouchEventHandler SwipeEvent;
- public static event TouchEventHandler SwipeEndEvent;
- Vector2 m_touchMovement;
- float m_minSwipeDistance = 25;
- float m_newSwipeDistance = 0;
- void OnSwipe()
- {
- if (SwipeEvent != null)
- {
- SwipeEvent(m_touchMovement);
- }
- }
- void OnSwipeEnd()
- {
- if (SwipeEndEvent != null)
- {
- SwipeEndEvent(m_touchMovement);
- }
- }
- // public Text m_diagnosticText1;
- // public Text m_diagnosticText2;
- public bool m_useDiagnostic = false;
- // Use this for initialization
- void Start()
- {
- Diagnostic("", "");
- }
- void Diagnostic(string text1, string text2)
- {
- //if (m_diagnosticText1 && m_diagnosticText2)
- //{
- // m_diagnosticText1.gameObject.SetActive(m_useDiagnostic);
- // m_diagnosticText2.gameObject.SetActive(m_useDiagnostic);
- //}
- //if (m_diagnosticText1 != null && m_diagnosticText2 != null)
- //{
- // m_diagnosticText1.text = text1;
- // m_diagnosticText2.text = text2;
- //}
- }
- string SwipeDiagnostic(Vector2 swipeMovement)
- {
- string direction = "";
- // horizontal
- if (Mathf.Abs(swipeMovement.x) > Mathf.Abs(swipeMovement.y))
- {
- direction = (swipeMovement.x >= 0) ? "right" : "left";
- }
- // vertical
- else
- {
- direction = (swipeMovement.y >= 0) ? "up" : "down";
- }
- return direction;
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.touchCount > 0)
- {
- Touch touch = Input.touches[0];
- if (touch.phase == TouchPhase.Began)
- {
- m_touchMovement = Vector2.zero;
- Diagnostic("", "");
- }
- else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
- {
- m_touchMovement += touch.deltaPosition;
- // m_newSwipeDistance = m_touchMovement.magnitude;
- if (m_touchMovement.magnitude > m_minSwipeDistance)
- {
- OnSwipe();
- Diagnostic("Swipe detected", m_touchMovement.ToString() + " " + SwipeDiagnostic(m_touchMovement));
- }
- else if(m_touchMovement.magnitude < m_minSwipeDistance/6)
- {
- m_touchMovement = Vector2.zero;
- }
- }
- else if (touch.phase == TouchPhase.Ended)
- {
- OnSwipeEnd();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment