@@ -47,10 +47,19 @@ public Vector2 IndicatorSidePos
47
47
public static Action < Vector3 , SideDirection , LeanFinger > OnTapScreenSide ;
48
48
public static Action < Vector3 , SideDirection , LeanFinger > OnTapScreenDiagonal ;
49
49
50
+ [ Header ( "Touch references" ) ]
51
+ [ Tooltip ( "The player reference to check the screen sides" ) ]
52
+ [ SerializeField ]
53
+ protected GameObject player ;
54
+
55
+ [ Tooltip ( "If player is in Isometric plan, the reference will be change Y => Z" ) ]
56
+ [ SerializeField ]
57
+ protected bool isIsometric = false ;
58
+
50
59
[ Tooltip ( "The camera to calculate where show gizmos, touch indicators and sides" ) ]
51
60
[ SerializeField ]
52
61
protected Camera cam ;
53
-
62
+
54
63
[ Header ( "Touch indicator" ) ]
55
64
[ Tooltip ( "Duration in seconds of touch indicator circle" ) ]
56
65
[ SerializeField ]
@@ -71,7 +80,7 @@ public Vector2 IndicatorSidePos
71
80
[ SerializeField ]
72
81
protected bool cancelMiddle = true ;
73
82
74
- [ Tooltip ( "How is the DISTANCE between the touch and the middle will be ignored? " ) ]
83
+ [ Tooltip ( "The X and Y values of the DISTANCE between the touch and the middle will be ignored" ) ]
75
84
[ SerializeField ]
76
85
protected Vector2 middleDistance = Vector2 . right ;
77
86
@@ -106,6 +115,29 @@ public Vector3 ScreenMiddlePos
106
115
}
107
116
}
108
117
118
+ public Vector3 ReferenceTouch
119
+ {
120
+ get
121
+ {
122
+ Vector3 reference = screenMiddleRaw ;
123
+
124
+ if ( player != null )
125
+ {
126
+ reference = player . transform . position ;
127
+
128
+ // TODO: Create a Editor script to show this field, only if player != null
129
+ if ( isIsometric )
130
+ {
131
+ reference . y = reference . z ;
132
+ }
133
+ }
134
+
135
+ return reference ;
136
+ }
137
+
138
+ protected set { }
139
+ }
140
+
109
141
void Awake ( )
110
142
{
111
143
fingerTap = GetComponentInChildren < LeanFingerTap > ( ) ;
@@ -210,7 +242,7 @@ void DrawTouchDiagonal()
210
242
IEnumerator DrawTouchIndicator ( float seconds = 2 )
211
243
{
212
244
float touchRadius = 0 ;
213
- Vector2 indicatorPosition = touchedFinger != null ? touchedFinger . ScreenPosition : Vector2 . zero ;
245
+
214
246
if ( touchedFinger != null && touchedFinger . IsActive )
215
247
{
216
248
if ( touchUI != null && touchUI . IgnoreUI )
@@ -227,7 +259,8 @@ IEnumerator DrawTouchIndicator(float seconds = 2)
227
259
touchRadius = GetTouchRadius ( ) ;
228
260
}
229
261
230
- Handles . DrawSolidDisc ( /*indicatorPosition*/ touchWorldPos , Vector3 . forward , touchRadius ) ;
262
+ //Draw a solid disc in the coordinates in a space in the game world
263
+ Handles . DrawSolidDisc ( touchWorldPos , Vector3 . forward , touchRadius ) ;
231
264
yield return new WaitForSeconds ( seconds ) ;
232
265
}
233
266
#endif
@@ -236,60 +269,67 @@ void OnEnable()
236
269
{
237
270
if ( fingerTap != null && fingerTap . isActiveAndEnabled )
238
271
{
239
- fingerTap . OnPosition . AddListener ( TapScreenPos ) ;
272
+ fingerTap . OnFinger . AddListener ( TapScreen ) ;
240
273
}
241
274
else
242
275
{
243
276
LeanTouch . OnFingerTap += TapScreen ;
244
277
}
245
278
279
+ #if UNITY_EDITOR
280
+ // Subscribe this event, only in editor (Gizmos debug)
246
281
LeanTouch . OnFingerSet += TapIndicator ;
282
+ #endif
247
283
}
248
284
249
285
void OnDisable ( )
250
286
{
251
287
if ( fingerTap != null && fingerTap . isActiveAndEnabled )
252
288
{
253
- fingerTap . OnPosition . RemoveListener ( TapScreenPos ) ;
289
+ fingerTap . OnFinger . RemoveListener ( TapScreen ) ;
254
290
}
255
291
else
256
292
{
257
293
LeanTouch . OnFingerTap -= TapScreen ;
258
294
}
259
295
296
+ #if UNITY_EDITOR
297
+ // Unsubscribe this event, only in editor (Gizmos debug)
260
298
LeanTouch . OnFingerSet -= TapIndicator ;
299
+ #endif
261
300
}
262
301
263
302
public virtual void TapScreen ( LeanFinger finger )
264
303
{
265
304
TapScreenVerify ( finger . LastScreenPosition , finger ) ;
266
305
}
267
306
268
- public virtual void TapScreenPos ( Vector3 position )
269
- {
270
- TapScreenVerify ( position ) ;
271
- }
272
-
273
- public virtual void TapScreenVerify ( Vector3 position , LeanFinger finger = null )
307
+ public virtual void TapScreenVerify ( Vector3 touchPosition , LeanFinger finger = null )
274
308
{
275
309
GameObject uiElement ;
276
- bool touchedInUI = finger != null ? touchUI . IsTouched ( finger , out uiElement ) : touchUI . IsTouched ( position , out uiElement ) ;
310
+ bool touchedInUI = finger != null ? touchUI . IsTouched ( finger , out uiElement ) : touchUI . IsTouched ( touchPosition , out uiElement ) ;
311
+
277
312
278
313
if ( touchedInUI )
279
314
{
280
- var direction = GetDirection ( position ) ;
315
+ var direction = GetDirection ( touchPosition ) ;
281
316
282
- LeanTouchUI . OnTapUI ? . Invoke ( position , direction , uiElement , finger ) ;
317
+ LeanTouchUI . OnTapUI ? . Invoke ( touchPosition , direction , uiElement , finger ) ;
283
318
} else
284
319
{
285
- var callbackEvent = GetDirectionAction ( position ) ;
286
- callbackEvent ? . Invoke ( position , finger ) ;
320
+ var callbackEvent = GetDirectionAction ( touchPosition ) ;
321
+ callbackEvent ? . Invoke ( touchPosition , finger ) ;
287
322
}
288
323
}
289
324
325
+ /// <summary>
326
+ /// Get touch data (touch world position, direction...)
327
+ ///<b>PS:</b> This is only to debug
328
+ /// </summary>
329
+ /// <param name="finger">The last touch touch information object</param>
290
330
public virtual void TapIndicator ( LeanFinger finger )
291
331
{
292
- touchWorldPos = cam . ScreenToWorldPoint ( finger . StartScreenPosition ) ;
332
+ touchWorldPos = cam . ScreenToWorldPoint ( finger . LastScreenPosition ) ;
293
333
294
334
touchedFinger = finger ;
295
335
lastTouchedDirection = GetDirection ( touchWorldPos ) ;
@@ -342,28 +382,29 @@ public virtual float GetTouchRadius()
342
382
return radius ;
343
383
}
344
384
345
- public SideDirection IsScreenLeft ( Vector2 touchPosition )
385
+ public SideDirection IsScreenLeft ( Vector3 touchPosition )
346
386
{
387
+
347
388
if ( cancelMiddle && IsScreenMiddle ( touchPosition ) . Equals ( SideDirection . MIDDLE ) )
348
389
{
349
390
return SideDirection . NONE ;
350
391
}
351
392
352
- if ( touchPosition != Vector2 . zero )
393
+ if ( touchPosition != Vector3 . zero )
353
394
{
354
395
/**
355
396
* Left: Only change "lastTouchedDirection" property when the
356
397
* direction is not equal to SideDirection.NONE
357
398
*
358
399
* This to not impact the Right directions
359
400
*/
360
- if ( touchPosition . x < ScreenMiddlePos . x )
401
+ if ( touchPosition . x < ReferenceTouch . x )
361
402
{
362
- if ( diagonals && touchPosition . y > ScreenMiddlePos . y )
403
+ if ( diagonals && touchPosition . y > ReferenceTouch . y )
363
404
{
364
405
lastTouchedDirection = SideDirection . TOP_LEFT ;
365
406
}
366
- else if ( diagonals && touchPosition . y < ScreenMiddlePos . y )
407
+ else if ( diagonals && touchPosition . y < ReferenceTouch . y )
367
408
{
368
409
lastTouchedDirection = SideDirection . DOWN_LEFT ;
369
410
}
@@ -379,25 +420,25 @@ public SideDirection IsScreenLeft(Vector2 touchPosition)
379
420
return SideDirection . NONE ;
380
421
}
381
422
382
- public SideDirection IsScreenRight ( Vector2 touchPosition )
423
+ public SideDirection IsScreenRight ( Vector3 touchPosition )
383
424
{
384
425
385
426
if ( cancelMiddle && IsScreenMiddle ( touchPosition ) . Equals ( SideDirection . MIDDLE ) )
386
427
{
387
428
return SideDirection . NONE ;
388
429
}
389
430
390
- if ( touchPosition != Vector2 . zero )
431
+ if ( touchPosition != Vector3 . zero )
391
432
{
392
433
393
434
// Right
394
- if ( touchPosition . x > ScreenMiddlePos . x )
435
+ if ( touchPosition . x > ReferenceTouch . x )
395
436
{
396
- if ( diagonals && touchPosition . y > ScreenMiddlePos . y )
437
+ if ( diagonals && touchPosition . y > ReferenceTouch . y )
397
438
{
398
439
lastTouchedDirection = SideDirection . TOP_RIGHT ;
399
440
}
400
- else if ( diagonals && touchPosition . y < ScreenMiddlePos . y )
441
+ else if ( diagonals && touchPosition . y < ReferenceTouch . y )
401
442
{
402
443
lastTouchedDirection = SideDirection . DOWN_RIGHT ;
403
444
}
@@ -435,13 +476,13 @@ public SideDirection IsScreenMiddle(Vector2 touchPosition, (bool? x, bool? y) ch
435
476
if ( x )
436
477
{
437
478
posForDistance . touch . x = touchPosition . x ;
438
- posForDistance . screen . x = ScreenMiddlePos . x ;
479
+ posForDistance . screen . x = ReferenceTouch . x ;
439
480
}
440
481
441
482
if ( y )
442
483
{
443
484
posForDistance . touch . y = touchPosition . y ;
444
- posForDistance . screen . y = ScreenMiddlePos . y ;
485
+ posForDistance . screen . y = ReferenceTouch . y ;
445
486
}
446
487
447
488
/**
0 commit comments