Skip to content

Commit 145c80a

Browse files
committed
[Touch UI] Identify when UI gameObject is touched
1 parent ea09039 commit 145c80a

File tree

5 files changed

+242
-36
lines changed

5 files changed

+242
-36
lines changed

Runtime/LeanTouchScreen.cs

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public class LeanTouchScreen : MonoBehaviour
1212
{
1313
public enum SideDirection
1414
{
15-
1615
NONE,
1716
MIDDLE,
1817
LEFT,
@@ -27,47 +26,67 @@ public class DirectionData
2726
{
2827
public DirectionData() { }
2928

30-
public Vector2 IndicatorSidePos {
29+
public Vector2 IndicatorSidePos
30+
{
3131
get
3232
{
3333
return sidePos;
3434
}
35-
set
35+
set
3636
{
3737
sidePos = value;
38-
}
38+
}
3939
}
4040
public Action<Vector3, LeanFinger> OnTapDirection { get; set; }
4141

4242
protected Vector2 sidePos = Vector2.zero;
4343

4444
}
4545

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+
4654
[Header("Touch indicator")]
4755
[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;
4963

5064
[Header("Directions")]
5165
[Tooltip("Check directions: Top + Left/Right and Down + Left/Right?")]
52-
public bool diagonals = true;
66+
[SerializeField]
67+
protected bool diagonals = true;
5368

5469
[Header("Middle configuration")]
5570
[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;
5873

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;
6281

6382
protected Vector3 touchWorldPos = Vector3.zero;
6483
protected SideDirection lastTouchedDirection = SideDirection.NONE;
6584

6685
// --- Components ----
6786
protected LeanFinger touchedFinger;
6887
protected LeanFingerTap fingerTap;
88+
protected LeanTouchUI touchUI;
6989
protected UnityEngine.Touch touch;
70-
protected Camera cam;
7190

7291
private Vector3 screenMiddleRaw = new Vector2(Screen.width / 2, Screen.height / 2);
7392
private Vector3 screenWorldPos = Vector3.zero;
@@ -76,9 +95,10 @@ public Vector2 IndicatorSidePos {
7695

7796
public Vector3 ScreenMiddlePos
7897
{
79-
get {
98+
get
99+
{
80100
screenMiddleRaw = new Vector2(Screen.width / 2, Screen.height / 2);
81-
if (fingerTap != null)
101+
if (fingerTap != null && fingerTap.isActiveAndEnabled)
82102
{
83103
return fingerTap.ScreenDepth.Convert(screenMiddleRaw, fingerTap.gameObject);
84104
}
@@ -89,8 +109,14 @@ public Vector3 ScreenMiddlePos
89109
void Awake()
90110
{
91111
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+
};
92119

93-
cam = LeanTouch.GetCamera(null);
94120
screenWorldPos = cam.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
95121

96122
directionsActions = new Dictionary<SideDirection, DirectionData>()
@@ -109,11 +135,11 @@ void Awake()
109135
IndicatorSidePos = new Vector2(screenWorldPos.x, screenWorldPos.y),
110136
OnTapDirection = (Vector3 pos, LeanFinger finger) => OnTapScreenDiagonal?.Invoke(pos, SideDirection.TOP_RIGHT, finger)
111137
} },
112-
{ SideDirection.DOWN_LEFT, new DirectionData {
138+
{ SideDirection.DOWN_LEFT, new DirectionData {
113139
IndicatorSidePos = new Vector2(-screenWorldPos.x, -screenWorldPos.y),
114140
OnTapDirection = (Vector3 pos, LeanFinger finger) => OnTapScreenDiagonal?.Invoke(pos, SideDirection.DOWN_LEFT, finger)
115141
} },
116-
{ SideDirection.DOWN_RIGHT, new DirectionData {
142+
{ SideDirection.DOWN_RIGHT, new DirectionData {
117143
IndicatorSidePos = new Vector2(screenWorldPos.x, -screenWorldPos.y),
118144
OnTapDirection = (Vector3 pos, LeanFinger finger) => OnTapScreenDiagonal?.Invoke(pos, SideDirection.DOWN_RIGHT, finger)
119145
} }
@@ -156,7 +182,7 @@ void DrawTouchDiagonal()
156182

157183
if (diagonals && screenWorldPos != Vector3.zero && (touchedFinger != null && touchedFinger.IsActive))
158184
{
159-
if (fingerTap != null && (fingerTap.IgnoreIsOverGui || fingerTap.IgnoreStartedOverGui) && touchedFinger.IsOverGui)
185+
if (touchUI != null && touchUI.IgnoreUI)
160186
{
161187
return;
162188
}
@@ -183,10 +209,11 @@ void DrawTouchDiagonal()
183209

184210
IEnumerator DrawTouchIndicator(float seconds = 2)
185211
{
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)
188215
{
189-
if (fingerTap != null && (fingerTap.IgnoreIsOverGui || fingerTap.IgnoreStartedOverGui) && touchedFinger.IsOverGui)
216+
if (touchUI != null && touchUI.IgnoreUI)
190217
{
191218
Color ignoreColor = Color.red;
192219
ignoreColor.a = 0.3f;
@@ -197,20 +224,21 @@ IEnumerator DrawTouchIndicator(float seconds = 2)
197224
Handles.color = Color.green;
198225
}
199226

200-
radius = GetTouchRadius();
227+
touchRadius = GetTouchRadius();
201228
}
202229

203-
Handles.DrawSolidDisc(touchWorldPos, Vector3.forward, radius);
230+
Handles.DrawSolidDisc(/*indicatorPosition*/ touchWorldPos, Vector3.forward, touchRadius);
204231
yield return new WaitForSeconds(seconds);
205232
}
206233
#endif
207234

208235
void OnEnable()
209236
{
210-
if (fingerTap != null)
237+
if (fingerTap != null && fingerTap.isActiveAndEnabled)
211238
{
212239
fingerTap.OnPosition.AddListener(TapScreenPos);
213-
} else
240+
}
241+
else
214242
{
215243
LeanTouch.OnFingerTap += TapScreen;
216244
}
@@ -220,7 +248,7 @@ void OnEnable()
220248

221249
void OnDisable()
222250
{
223-
if (fingerTap != null)
251+
if (fingerTap != null && fingerTap.isActiveAndEnabled)
224252
{
225253
fingerTap.OnPosition.RemoveListener(TapScreenPos);
226254
}
@@ -244,15 +272,26 @@ public virtual void TapScreenPos(Vector3 position)
244272

245273
public virtual void TapScreenVerify(Vector3 position, LeanFinger finger = null)
246274
{
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+
}
249288
}
250289

251290
public virtual void TapIndicator(LeanFinger finger)
252291
{
253-
touchWorldPos = cam.ScreenToWorldPoint(finger.LastScreenPosition);
254-
touchedFinger = finger;
292+
touchWorldPos = cam.ScreenToWorldPoint(finger.StartScreenPosition);
255293

294+
touchedFinger = finger;
256295
lastTouchedDirection = GetDirection(touchWorldPos);
257296
}
258297

@@ -293,7 +332,6 @@ public virtual SideDirection GetDirection(Vector3 position)
293332

294333
public virtual float GetTouchRadius()
295334
{
296-
float radius = 1;
297335
touch = GetTouch();
298336

299337
if (touch.radius > 0)
@@ -324,10 +362,12 @@ public SideDirection IsScreenLeft(Vector2 touchPosition)
324362
if (diagonals && touchPosition.y > ScreenMiddlePos.y)
325363
{
326364
lastTouchedDirection = SideDirection.TOP_LEFT;
327-
} else if (diagonals && touchPosition.y < ScreenMiddlePos.y)
365+
}
366+
else if (diagonals && touchPosition.y < ScreenMiddlePos.y)
328367
{
329368
lastTouchedDirection = SideDirection.DOWN_LEFT;
330-
} else
369+
}
370+
else
331371
{
332372
lastTouchedDirection = SideDirection.LEFT;
333373
}
@@ -368,7 +408,7 @@ public SideDirection IsScreenRight(Vector2 touchPosition)
368408

369409
return lastTouchedDirection;
370410
}
371-
411+
372412
}
373413

374414
return SideDirection.NONE;

0 commit comments

Comments
 (0)