Skip to content

Commit

Permalink
[Vibration] Added vibration on tap and editor configurations
Browse files Browse the repository at this point in the history
Using Vibration mobile plugin upm
  • Loading branch information
mfdeveloper committed Jun 10, 2020
1 parent 4dfe01d commit d56ebc5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Runtime/Lean.Touch.Extensions.ScreenAreas.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "Lean.Touch.Extensions.ScreenAreas",
"references": [
"GUID:e9745f6a32442194c8dc5a43e9ab86f9",
"GUID:db3202cb94ce05e4a9fea1c0c4636177"
"GUID:db3202cb94ce05e4a9fea1c0c4636177",
"GUID:742764898e7ebba4c94b9d41bbb0f72c"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
46 changes: 44 additions & 2 deletions Runtime/LeanTouchScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
using UnityEditor;
#endif

[Serializable]
public struct VibrationOnElements
{
public bool touchScreen, uiElement;
}

namespace Lean.Touch.Extensions.ScreenAreas
{
public class LeanTouchScreen : MonoBehaviour
Expand Down Expand Up @@ -59,7 +65,12 @@ public Vector2 IndicatorSidePos
[Tooltip("The camera to calculate where show gizmos, touch indicators and sides")]
[SerializeField]
protected Camera cam;


[Header("Vibration")]
[Tooltip("When will be vibrate on touch?")]
[SerializeField]
protected VibrationOnElements vibrateOn;

[Header("Touch indicator")]
[Tooltip("Duration in seconds of touch indicator circle")]
[SerializeField]
Expand Down Expand Up @@ -96,12 +107,14 @@ public Vector2 IndicatorSidePos
protected LeanFingerTap fingerTap;
protected LeanTouchUI touchUI;
protected UnityEngine.Touch touch;
protected VibrationComponent vibration;

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

private Dictionary<SideDirection, DirectionData> directionsActions = default;

[Obsolete("ScreenMiddlePos is deprecated and will be removed in future versions. Use 'screenMidleRaw' instead", true)]
public Vector3 ScreenMiddlePos
{
get
Expand Down Expand Up @@ -138,11 +151,22 @@ public Vector3 ReferenceTouch
protected set { }
}


void Awake()
{
fingerTap = GetComponentInChildren<LeanFingerTap>();
layerMask = layerMask.value == LayerMask.NameToLayer("Default") ? LayerMask.NameToLayer("UI") : layerMask.value;
cam = LeanTouch.GetCamera(cam);
vibration = GetComponent<VibrationComponent>();

if (vibrateOn.touchScreen || vibrateOn.uiElement)
{
if (vibration == null)
{
string msg = "The vibrate on touch is enabled, but you not add '{0}' component. Add it to '{1}' gameObject";
Debug.LogErrorFormat(msg, "VibrationCustom", gameObject.name.ToUpper());
}
}

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


if (touchedInUI)
{
var direction = GetDirection(touchPosition);

LeanTouchUI.OnTapUI?.Invoke(touchPosition, direction, uiElement, finger);

if (vibrateOn.uiElement)
{
if (vibration != null)
{
var milliseconds = Convert.ToInt64(TimeSpan.FromSeconds(vibration.duration).TotalMilliseconds);
vibration.Vibrate(milliseconds);
}
}
} else
{
var callbackEvent = GetDirectionAction(touchPosition);
callbackEvent?.Invoke(touchPosition, finger);

if (vibrateOn.touchScreen)
{
if (vibration != null)
{
var milliseconds = Convert.ToInt64(TimeSpan.FromSeconds(vibration.duration).TotalMilliseconds);
vibration.Vibrate(milliseconds);
}
}
}

}

/// <summary>
Expand Down

0 comments on commit d56ebc5

Please sign in to comment.