|
6 | 6 | using UnityEditor;
|
7 | 7 | #endif
|
8 | 8 |
|
| 9 | +[Serializable] |
| 10 | +public struct VibrationOnElements |
| 11 | +{ |
| 12 | + public bool touchScreen, uiElement; |
| 13 | +} |
| 14 | + |
9 | 15 | namespace Lean.Touch.Extensions.ScreenAreas
|
10 | 16 | {
|
11 | 17 | public class LeanTouchScreen : MonoBehaviour
|
@@ -59,7 +65,12 @@ public Vector2 IndicatorSidePos
|
59 | 65 | [Tooltip("The camera to calculate where show gizmos, touch indicators and sides")]
|
60 | 66 | [SerializeField]
|
61 | 67 | protected Camera cam;
|
62 |
| - |
| 68 | + |
| 69 | + [Header("Vibration")] |
| 70 | + [Tooltip("When will be vibrate on touch?")] |
| 71 | + [SerializeField] |
| 72 | + protected VibrationOnElements vibrateOn; |
| 73 | + |
63 | 74 | [Header("Touch indicator")]
|
64 | 75 | [Tooltip("Duration in seconds of touch indicator circle")]
|
65 | 76 | [SerializeField]
|
@@ -96,12 +107,14 @@ public Vector2 IndicatorSidePos
|
96 | 107 | protected LeanFingerTap fingerTap;
|
97 | 108 | protected LeanTouchUI touchUI;
|
98 | 109 | protected UnityEngine.Touch touch;
|
| 110 | + protected VibrationComponent vibration; |
99 | 111 |
|
100 | 112 | private Vector3 screenMiddleRaw = new Vector2(Screen.width / 2, Screen.height / 2);
|
101 | 113 | private Vector3 screenWorldPos = Vector3.zero;
|
102 | 114 |
|
103 | 115 | private Dictionary<SideDirection, DirectionData> directionsActions = default;
|
104 | 116 |
|
| 117 | + [Obsolete("ScreenMiddlePos is deprecated and will be removed in future versions. Use 'screenMidleRaw' instead", true)] |
105 | 118 | public Vector3 ScreenMiddlePos
|
106 | 119 | {
|
107 | 120 | get
|
@@ -138,11 +151,22 @@ public Vector3 ReferenceTouch
|
138 | 151 | protected set { }
|
139 | 152 | }
|
140 | 153 |
|
| 154 | + |
141 | 155 | void Awake()
|
142 | 156 | {
|
143 | 157 | fingerTap = GetComponentInChildren<LeanFingerTap>();
|
144 | 158 | layerMask = layerMask.value == LayerMask.NameToLayer("Default") ? LayerMask.NameToLayer("UI") : layerMask.value;
|
145 | 159 | 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 | + } |
146 | 170 |
|
147 | 171 | touchUI = new LeanTouchUI {
|
148 | 172 | FingerTap = fingerTap,
|
@@ -309,17 +333,35 @@ public virtual void TapScreenVerify(Vector3 touchPosition, LeanFinger finger = n
|
309 | 333 | GameObject uiElement;
|
310 | 334 | bool touchedInUI = finger != null ? touchUI.IsTouched(finger, out uiElement) : touchUI.IsTouched(touchPosition, out uiElement);
|
311 | 335 |
|
312 |
| - |
313 | 336 | if (touchedInUI)
|
314 | 337 | {
|
315 | 338 | var direction = GetDirection(touchPosition);
|
316 | 339 |
|
317 | 340 | 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 | + } |
318 | 350 | } else
|
319 | 351 | {
|
320 | 352 | var callbackEvent = GetDirectionAction(touchPosition);
|
321 | 353 | 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 | + } |
322 | 363 | }
|
| 364 | + |
323 | 365 | }
|
324 | 366 |
|
325 | 367 | /// <summary>
|
|
0 commit comments