using UnityEngine;
using FSA = UnityEngine.Serialization.FormerlySerializedAsAttribute;
using Lean.Common;
namespace Lean.Touch
{
/// This component allows you to translate the current GameObject relative to the camera using the finger drag gesture.
[HelpURL(LeanTouch.HelpUrlPrefix + "LeanDragTranslate")]
[AddComponentMenu(LeanTouch.ComponentPathPrefix + "Drag Translate")]
public class LeanDragTranslate : MonoBehaviour
{
/// The method used to find fingers to use with this component. See LeanFingerFilter documentation for more information.
public LeanFingerFilter Use = new LeanFingerFilter(true);
/// The camera the translation will be calculated using.
/// None/null = MainCamera.
public Camera Camera { set { _camera = value; } get { return _camera; } } [FSA("Camera")] [SerializeField] private Camera _camera;
/// The movement speed will be multiplied by this.
/// -1 = Inverted Controls.
public float Sensitivity { set { sensitivity = value; } get { return sensitivity; } } [FSA("Sensitivity")] [SerializeField] private float sensitivity = 1.0f;
/// If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value.
/// -1 = Instantly change.
/// 1 = Slowly change.
/// 10 = Quickly change.
public float Damping { set { damping = value; } get { return damping; } } [FSA("Damping")] [FSA("Dampening")] [SerializeField] private float damping = -1.0f;
/// This allows you to control how much momentum is retained when the dragging fingers are all released.
/// NOTE: This requires Dampening to be above 0.
public float Inertia { set { inertia = value; } get { return inertia; } } [FSA("Inertia")] [SerializeField] [Range(0.0f, 1.0f)] private float inertia;
[SerializeField]
private Vector3 remainingTranslation;
/// If you've set Use to ManuallyAddedFingers, then you can call this method to manually add a finger.
public void AddFinger(LeanFinger finger)
{
Use.AddFinger(finger);
}
/// If you've set Use to ManuallyAddedFingers, then you can call this method to manually remove a finger.
public void RemoveFinger(LeanFinger finger)
{
Use.RemoveFinger(finger);
}
/// If you've set Use to ManuallyAddedFingers, then you can call this method to manually remove all fingers.
public void RemoveAllFingers()
{
Use.RemoveAllFingers();
}
#if UNITY_EDITOR
protected virtual void Reset()
{
Use.UpdateRequiredSelectable(gameObject);
}
#endif
protected virtual void Awake()
{
Use.UpdateRequiredSelectable(gameObject);
}
protected virtual void Update()
{
// Store
var oldPosition = transform.localPosition;
// Get the fingers we want to use
var fingers = Use.UpdateAndGetFingers();
// Calculate the screenDelta value based on these fingers
var screenDelta = LeanGesture.GetScreenDelta(fingers);
if (screenDelta != Vector2.zero)
{
// Perform the translation
if (transform is RectTransform)
{
TranslateUI(screenDelta);
}
else
{
Translate(screenDelta);
}
}
// Increment
remainingTranslation += transform.localPosition - oldPosition;
// Get t value
var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);
// Dampen remainingDelta
var newRemainingTranslation = Vector3.Lerp(remainingTranslation, Vector3.zero, factor);
// Shift this transform by the change in delta
transform.localPosition = oldPosition + remainingTranslation - newRemainingTranslation;
if (fingers.Count == 0 && Inertia > 0.0f && Damping > 0.0f)
{
newRemainingTranslation = Vector3.Lerp(newRemainingTranslation, remainingTranslation, Inertia);
}
// Update remainingDelta with the dampened value
remainingTranslation = newRemainingTranslation;
}
private void TranslateUI(Vector2 screenDelta)
{
var camera = this._camera;
if (camera == null)
{
var canvas = transform.GetComponentInParent