标签:
我使用Input的Touch和EasyTouch各实现了滑屏方案,基本原理就是得到滑屏移动时的二维向量,通过向量获取究竟是向哪个方向滑动,通过自定义的状态来实现。下面上代码:
下面是EasyTouch实现的:
1 using UnityEngine; 2 using System.Collections; 3 4 public enum SwipeDir 5 { 6 idle, //没有滑动 7 left, 8 right, 9 up, 10 down 11 } 12 13 public class MyEasyTouch : MonoBehaviour 14 { 15 [HideInInspector] 16 public SwipeDir swipeDir = SwipeDir.idle; 17 Vector2 swipeVector; 18 19 public bool isLeftSwipe = false; 20 public bool isRightSwipe = false; 21 public bool isUpSwipe = false; 22 public bool isDownSwipe = false; 23 24 25 void OnEnable() 26 { 27 //EasyTouch.On_Swipe += OnSwipe; 28 EasyTouch.On_SwipeEnd += OnSwipe; 29 } 30 31 void OnDisable() 32 { 33 //EasyTouch.On_Swipe -= OnSwipe; 34 EasyTouch.On_SwipeEnd -= OnSwipe; 35 36 37 } 38 39 void OnDestroy() 40 { 41 EasyTouch.On_SwipeEnd -= OnSwipe; 42 } 43 44 public void OnSwipe(Gesture gesture) 45 { 46 swipeVector = gesture.swipeVector; 47 switch (GetCurrentSwipeDirection()) 48 { 49 case SwipeDir.left: 50 isLeftSwipe = true; 51 break; 52 case SwipeDir.right: 53 isRightSwipe = true; 54 break; 55 case SwipeDir.up: 56 isUpSwipe = true; 57 break; 58 case SwipeDir.down: 59 isDownSwipe = true; 60 break; 61 62 } 63 } 64 65 public SwipeDir GetCurrentSwipeDirection() 66 { 67 if (Mathf.Abs(swipeVector.x) > Mathf.Abs(swipeVector.y)) 68 { 69 if (swipeVector.x > 0) 70 { 71 return SwipeDir.right; 72 } 73 else if (swipeVector.x < 0) 74 { 75 return SwipeDir.left; 76 } 77 } 78 if (Mathf.Abs(swipeVector.x) < Mathf.Abs(swipeVector.y)) 79 { 80 if (swipeVector.y > 0) 81 { 82 return SwipeDir.up; 83 } 84 else if (swipeVector.y < 0) 85 { 86 return SwipeDir.down; 87 } 88 } 89 90 return SwipeDir.idle; 91 } 92 93 }
下面是Unity自带的Touch类实现的:
1 using UnityEngine; 2 using System.Collections; 3 4 enum TouchMoveDir 5 { 6 idle,left,right,up,down 7 } 8 9 public class TouchTest : MonoBehaviour { 10 public GameObject target; 11 float minDis = 1; 12 TouchMoveDir moveDir; 13 14 // Use this for initialization 15 void Start () { 16 Input.multiTouchEnabled = true; 17 Input.simulateMouseWithTouches = true; 18 } 19 20 // Update is called once per frame 21 void Update () { 22 if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) 23 { 24 if(Input.GetTouch(0).deltaPosition.sqrMagnitude > minDis) 25 { 26 Vector2 deltaDir = Input.GetTouch(0).deltaPosition; 27 if(Mathf.Abs(deltaDir.x) > Mathf.Abs(deltaDir.y)) 28 { 29 moveDir = deltaDir.x>0?TouchMoveDir.right:TouchMoveDir.left; 30 } 31 if(Mathf.Abs(deltaDir.y) > Mathf.Abs(deltaDir.x)) 32 { 33 moveDir = deltaDir.y>0?TouchMoveDir.up:TouchMoveDir.down; 34 } 35 } 36 } 37 38 if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) { 39 moveDir = TouchMoveDir.idle; 40 } 41 42 if (moveDir == TouchMoveDir.right) { 43 Debug.Log("right"); 44 target.transform.position += transform.right * 0.2f; 45 } 46 if (moveDir == TouchMoveDir.left) { 47 Debug.Log("left"); 48 target.transform.position -= transform.right * 0.2f; 49 } 50 if (moveDir == TouchMoveDir.up) { 51 target.transform.position += transform.up * 0.2f; 52 } 53 if (moveDir == TouchMoveDir.down) { 54 target.transform.position -= transform.up * 0.2f; 55 } 56 } 57 }
标签:
原文地址:http://www.cnblogs.com/kylinxue/p/4582044.html