Skip to content

Commit d56ebc5

Browse files
committed
[Vibration] Added vibration on tap and editor configurations
Using Vibration mobile plugin upm
1 parent 4dfe01d commit d56ebc5

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

Runtime/Lean.Touch.Extensions.ScreenAreas.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "Lean.Touch.Extensions.ScreenAreas",
33
"references": [
44
"GUID:e9745f6a32442194c8dc5a43e9ab86f9",
5-
"GUID:db3202cb94ce05e4a9fea1c0c4636177"
5+
"GUID:db3202cb94ce05e4a9fea1c0c4636177",
6+
"GUID:742764898e7ebba4c94b9d41bbb0f72c"
67
],
78
"includePlatforms": [],
89
"excludePlatforms": [],

Runtime/LeanTouchScreen.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
using UnityEditor;
77
#endif
88

9+
[Serializable]
10+
public struct VibrationOnElements
11+
{
12+
public bool touchScreen, uiElement;
13+
}
14+
915
namespace Lean.Touch.Extensions.ScreenAreas
1016
{
1117
public class LeanTouchScreen : MonoBehaviour
@@ -59,7 +65,12 @@ public Vector2 IndicatorSidePos
5965
[Tooltip("The camera to calculate where show gizmos, touch indicators and sides")]
6066
[SerializeField]
6167
protected Camera cam;
62-
68+
69+
[Header("Vibration")]
70+
[Tooltip("When will be vibrate on touch?")]
71+
[SerializeField]
72+
protected VibrationOnElements vibrateOn;
73+
6374
[Header("Touch indicator")]
6475
[Tooltip("Duration in seconds of touch indicator circle")]
6576
[SerializeField]
@@ -96,12 +107,14 @@ public Vector2 IndicatorSidePos
96107
protected LeanFingerTap fingerTap;
97108
protected LeanTouchUI touchUI;
98109
protected UnityEngine.Touch touch;
110+
protected VibrationComponent vibration;
99111

100112
private Vector3 screenMiddleRaw = new Vector2(Screen.width / 2, Screen.height / 2);
101113
private Vector3 screenWorldPos = Vector3.zero;
102114

103115
private Dictionary<SideDirection, DirectionData> directionsActions = default;
104116

117+
[Obsolete("ScreenMiddlePos is deprecated and will be removed in future versions. Use 'screenMidleRaw' instead", true)]
105118
public Vector3 ScreenMiddlePos
106119
{
107120
get
@@ -138,11 +151,22 @@ public Vector3 ReferenceTouch
138151
protected set { }
139152
}
140153

154+
141155
void Awake()
142156
{
143157
fingerTap = GetComponentInChildren<LeanFingerTap>();
144158
layerMask = layerMask.value == LayerMask.NameToLayer("Default") ? LayerMask.NameToLayer("UI") : layerMask.value;
145159
cam = LeanTouch.GetCamera(cam);
160+
vibration = GetComponent<VibrationComponent>();
161+
162+
if (vibrateOn.touchScreen || vibrateOn.uiElement)
163+
{
164+
if (vibration == null)
165+
{
166+
string msg = "The vibrate on touch is enabled, but you not add '{0}' component. Add it to '{1}' gameObject";
167+
Debug.LogErrorFormat(msg, "VibrationCustom", gameObject.name.ToUpper());
168+
}
169+
}
146170

147171
touchUI = new LeanTouchUI {
148172
FingerTap = fingerTap,
@@ -309,17 +333,35 @@ public virtual void TapScreenVerify(Vector3 touchPosition, LeanFinger finger = n
309333
GameObject uiElement;
310334
bool touchedInUI = finger != null ? touchUI.IsTouched(finger, out uiElement) : touchUI.IsTouched(touchPosition, out uiElement);
311335

312-
313336
if (touchedInUI)
314337
{
315338
var direction = GetDirection(touchPosition);
316339

317340
LeanTouchUI.OnTapUI?.Invoke(touchPosition, direction, uiElement, finger);
341+
342+
if (vibrateOn.uiElement)
343+
{
344+
if (vibration != null)
345+
{
346+
var milliseconds = Convert.ToInt64(TimeSpan.FromSeconds(vibration.duration).TotalMilliseconds);
347+
vibration.Vibrate(milliseconds);
348+
}
349+
}
318350
} else
319351
{
320352
var callbackEvent = GetDirectionAction(touchPosition);
321353
callbackEvent?.Invoke(touchPosition, finger);
354+
355+
if (vibrateOn.touchScreen)
356+
{
357+
if (vibration != null)
358+
{
359+
var milliseconds = Convert.ToInt64(TimeSpan.FromSeconds(vibration.duration).TotalMilliseconds);
360+
vibration.Vibrate(milliseconds);
361+
}
362+
}
322363
}
364+
323365
}
324366

325367
/// <summary>

0 commit comments

Comments
 (0)