@@ -12,7 +12,6 @@ public class LeanTouchScreen : MonoBehaviour
12
12
{
13
13
public enum SideDirection
14
14
{
15
-
16
15
NONE ,
17
16
MIDDLE ,
18
17
LEFT ,
@@ -27,47 +26,67 @@ public class DirectionData
27
26
{
28
27
public DirectionData ( ) { }
29
28
30
- public Vector2 IndicatorSidePos {
29
+ public Vector2 IndicatorSidePos
30
+ {
31
31
get
32
32
{
33
33
return sidePos ;
34
34
}
35
- set
35
+ set
36
36
{
37
37
sidePos = value ;
38
- }
38
+ }
39
39
}
40
40
public Action < Vector3 , LeanFinger > OnTapDirection { get ; set ; }
41
41
42
42
protected Vector2 sidePos = Vector2 . zero ;
43
43
44
44
}
45
45
46
+ // ---- Events ----
47
+ public static Action < Vector3 , SideDirection , LeanFinger > OnTapScreenSide ;
48
+ public static Action < Vector3 , SideDirection , LeanFinger > OnTapScreenDiagonal ;
49
+
50
+ [ Tooltip ( "The camera to calculate where show gizmos, touch indicators and sides" ) ]
51
+ [ SerializeField ]
52
+ protected Camera cam ;
53
+
46
54
[ Header ( "Touch indicator" ) ]
47
55
[ Tooltip ( "Duration in seconds of touch indicator circle" ) ]
48
- public float duration = 2 ;
56
+ [ SerializeField ]
57
+ protected float duration = 2 ;
58
+
59
+ [ Tooltip ( "The size of the touch indicator circle" ) ]
60
+ [ SerializeField ]
61
+ [ Range ( 1 , 100 ) ]
62
+ protected float radius = 1 ;
49
63
50
64
[ Header ( "Directions" ) ]
51
65
[ Tooltip ( "Check directions: Top + Left/Right and Down + Left/Right?" ) ]
52
- public bool diagonals = true ;
66
+ [ SerializeField ]
67
+ protected bool diagonals = true ;
53
68
54
69
[ Header ( "Middle configuration" ) ]
55
70
[ Tooltip ( "Do nothing if a screen middle is touched" ) ]
56
- public bool cancelMiddle = true ;
57
- public Vector2 middleDistance = Vector2 . right ;
71
+ [ SerializeField ]
72
+ protected bool cancelMiddle = true ;
58
73
59
- // ---- Events ----
60
- public static Action < Vector3 , SideDirection , LeanFinger > OnTapScreenSide ;
61
- public static Action < Vector3 , SideDirection , LeanFinger > OnTapScreenDiagonal ;
74
+ [ Tooltip ( "How is the DISTANCE between the touch and the middle will be ignored?" ) ]
75
+ [ SerializeField ]
76
+ protected Vector2 middleDistance = Vector2 . right ;
77
+
78
+ [ Header ( "UI Touch" ) ]
79
+ [ Tooltip ( "The layers you want the raycast/overlap to hit." ) ]
80
+ public LayerMask layerMask = Physics . DefaultRaycastLayers ;
62
81
63
82
protected Vector3 touchWorldPos = Vector3 . zero ;
64
83
protected SideDirection lastTouchedDirection = SideDirection . NONE ;
65
84
66
85
// --- Components ----
67
86
protected LeanFinger touchedFinger ;
68
87
protected LeanFingerTap fingerTap ;
88
+ protected LeanTouchUI touchUI ;
69
89
protected UnityEngine . Touch touch ;
70
- protected Camera cam ;
71
90
72
91
private Vector3 screenMiddleRaw = new Vector2 ( Screen . width / 2 , Screen . height / 2 ) ;
73
92
private Vector3 screenWorldPos = Vector3 . zero ;
@@ -76,9 +95,10 @@ public Vector2 IndicatorSidePos {
76
95
77
96
public Vector3 ScreenMiddlePos
78
97
{
79
- get {
98
+ get
99
+ {
80
100
screenMiddleRaw = new Vector2 ( Screen . width / 2 , Screen . height / 2 ) ;
81
- if ( fingerTap != null )
101
+ if ( fingerTap != null && fingerTap . isActiveAndEnabled )
82
102
{
83
103
return fingerTap . ScreenDepth . Convert ( screenMiddleRaw , fingerTap . gameObject ) ;
84
104
}
@@ -89,8 +109,14 @@ public Vector3 ScreenMiddlePos
89
109
void Awake ( )
90
110
{
91
111
fingerTap = GetComponentInChildren < LeanFingerTap > ( ) ;
112
+ layerMask = layerMask . value == LayerMask . NameToLayer ( "Default" ) ? LayerMask . NameToLayer ( "UI" ) : layerMask . value ;
113
+ cam = LeanTouch . GetCamera ( cam ) ;
114
+
115
+ touchUI = new LeanTouchUI {
116
+ FingerTap = fingerTap ,
117
+ layerToRaycast = layerMask
118
+ } ;
92
119
93
- cam = LeanTouch . GetCamera ( null ) ;
94
120
screenWorldPos = cam . ScreenToWorldPoint ( new Vector2 ( Screen . width , Screen . height ) ) ;
95
121
96
122
directionsActions = new Dictionary < SideDirection , DirectionData > ( )
@@ -109,11 +135,11 @@ void Awake()
109
135
IndicatorSidePos = new Vector2 ( screenWorldPos . x , screenWorldPos . y ) ,
110
136
OnTapDirection = ( Vector3 pos , LeanFinger finger ) => OnTapScreenDiagonal ? . Invoke ( pos , SideDirection . TOP_RIGHT , finger )
111
137
} } ,
112
- { SideDirection . DOWN_LEFT , new DirectionData {
138
+ { SideDirection . DOWN_LEFT , new DirectionData {
113
139
IndicatorSidePos = new Vector2 ( - screenWorldPos . x , - screenWorldPos . y ) ,
114
140
OnTapDirection = ( Vector3 pos , LeanFinger finger ) => OnTapScreenDiagonal ? . Invoke ( pos , SideDirection . DOWN_LEFT , finger )
115
141
} } ,
116
- { SideDirection . DOWN_RIGHT , new DirectionData {
142
+ { SideDirection . DOWN_RIGHT , new DirectionData {
117
143
IndicatorSidePos = new Vector2 ( screenWorldPos . x , - screenWorldPos . y ) ,
118
144
OnTapDirection = ( Vector3 pos , LeanFinger finger ) => OnTapScreenDiagonal ? . Invoke ( pos , SideDirection . DOWN_RIGHT , finger )
119
145
} }
@@ -156,7 +182,7 @@ void DrawTouchDiagonal()
156
182
157
183
if ( diagonals && screenWorldPos != Vector3 . zero && ( touchedFinger != null && touchedFinger . IsActive ) )
158
184
{
159
- if ( fingerTap != null && ( fingerTap . IgnoreIsOverGui || fingerTap . IgnoreStartedOverGui ) && touchedFinger . IsOverGui )
185
+ if ( touchUI != null && touchUI . IgnoreUI )
160
186
{
161
187
return ;
162
188
}
@@ -183,10 +209,11 @@ void DrawTouchDiagonal()
183
209
184
210
IEnumerator DrawTouchIndicator ( float seconds = 2 )
185
211
{
186
- float radius = 0 ;
187
- if ( touchWorldPos != Vector3 . zero && ( touchedFinger != null && touchedFinger . IsActive ) )
212
+ float touchRadius = 0 ;
213
+ Vector2 indicatorPosition = touchedFinger != null ? touchedFinger . ScreenPosition : Vector2 . zero ;
214
+ if ( touchedFinger != null && touchedFinger . IsActive )
188
215
{
189
- if ( fingerTap != null && ( fingerTap . IgnoreIsOverGui || fingerTap . IgnoreStartedOverGui ) && touchedFinger . IsOverGui )
216
+ if ( touchUI != null && touchUI . IgnoreUI )
190
217
{
191
218
Color ignoreColor = Color . red ;
192
219
ignoreColor . a = 0.3f ;
@@ -197,20 +224,21 @@ IEnumerator DrawTouchIndicator(float seconds = 2)
197
224
Handles . color = Color . green ;
198
225
}
199
226
200
- radius = GetTouchRadius ( ) ;
227
+ touchRadius = GetTouchRadius ( ) ;
201
228
}
202
229
203
- Handles . DrawSolidDisc ( touchWorldPos , Vector3 . forward , radius ) ;
230
+ Handles . DrawSolidDisc ( /*indicatorPosition*/ touchWorldPos , Vector3 . forward , touchRadius ) ;
204
231
yield return new WaitForSeconds ( seconds ) ;
205
232
}
206
233
#endif
207
234
208
235
void OnEnable ( )
209
236
{
210
- if ( fingerTap != null )
237
+ if ( fingerTap != null && fingerTap . isActiveAndEnabled )
211
238
{
212
239
fingerTap . OnPosition . AddListener ( TapScreenPos ) ;
213
- } else
240
+ }
241
+ else
214
242
{
215
243
LeanTouch . OnFingerTap += TapScreen ;
216
244
}
@@ -220,7 +248,7 @@ void OnEnable()
220
248
221
249
void OnDisable ( )
222
250
{
223
- if ( fingerTap != null )
251
+ if ( fingerTap != null && fingerTap . isActiveAndEnabled )
224
252
{
225
253
fingerTap . OnPosition . RemoveListener ( TapScreenPos ) ;
226
254
}
@@ -244,15 +272,26 @@ public virtual void TapScreenPos(Vector3 position)
244
272
245
273
public virtual void TapScreenVerify ( Vector3 position , LeanFinger finger = null )
246
274
{
247
- var callbackEvent = GetDirectionAction ( position ) ;
248
- callbackEvent ? . Invoke ( position , finger ) ;
275
+ GameObject uiElement ;
276
+ bool touchedInUI = finger != null ? touchUI . IsTouched ( finger , out uiElement ) : touchUI . IsTouched ( position , out uiElement ) ;
277
+
278
+ if ( touchedInUI )
279
+ {
280
+ var direction = GetDirection ( position ) ;
281
+
282
+ LeanTouchUI . OnTapUI ? . Invoke ( position , direction , uiElement , finger ) ;
283
+ } else
284
+ {
285
+ var callbackEvent = GetDirectionAction ( position ) ;
286
+ callbackEvent ? . Invoke ( position , finger ) ;
287
+ }
249
288
}
250
289
251
290
public virtual void TapIndicator ( LeanFinger finger )
252
291
{
253
- touchWorldPos = cam . ScreenToWorldPoint ( finger . LastScreenPosition ) ;
254
- touchedFinger = finger ;
292
+ touchWorldPos = cam . ScreenToWorldPoint ( finger . StartScreenPosition ) ;
255
293
294
+ touchedFinger = finger ;
256
295
lastTouchedDirection = GetDirection ( touchWorldPos ) ;
257
296
}
258
297
@@ -293,7 +332,6 @@ public virtual SideDirection GetDirection(Vector3 position)
293
332
294
333
public virtual float GetTouchRadius ( )
295
334
{
296
- float radius = 1 ;
297
335
touch = GetTouch ( ) ;
298
336
299
337
if ( touch . radius > 0 )
@@ -324,10 +362,12 @@ public SideDirection IsScreenLeft(Vector2 touchPosition)
324
362
if ( diagonals && touchPosition . y > ScreenMiddlePos . y )
325
363
{
326
364
lastTouchedDirection = SideDirection . TOP_LEFT ;
327
- } else if ( diagonals && touchPosition . y < ScreenMiddlePos . y )
365
+ }
366
+ else if ( diagonals && touchPosition . y < ScreenMiddlePos . y )
328
367
{
329
368
lastTouchedDirection = SideDirection . DOWN_LEFT ;
330
- } else
369
+ }
370
+ else
331
371
{
332
372
lastTouchedDirection = SideDirection . LEFT ;
333
373
}
@@ -368,7 +408,7 @@ public SideDirection IsScreenRight(Vector2 touchPosition)
368
408
369
409
return lastTouchedDirection ;
370
410
}
371
-
411
+
372
412
}
373
413
374
414
return SideDirection . NONE ;
0 commit comments