我使用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 }
MyEasyTouch

相关文章:

  • 2021-07-04
  • 2021-07-12
  • 2021-06-09
  • 2021-12-24
  • 2022-01-20
  • 2021-04-05
  • 2022-12-23
  • 2021-08-11
猜你喜欢
  • 2022-01-11
  • 2022-12-23
  • 2021-12-11
  • 2022-01-25
相关资源
相似解决方案