using UnityEngine; using UnityEngine.Events; using Lean.Common; using FSA = UnityEngine.Serialization.FormerlySerializedAsAttribute; namespace Lean.Touch { /// This component allows you to get the pinch of all fingers. [HelpURL(LeanTouch.PlusHelpUrlPrefix + "LeanMultiDirection")] [AddComponentMenu(LeanTouch.ComponentPathPrefix + "Multi Direction")] public class LeanMultiDirection : MonoBehaviour { public enum CoordinateType { ScaledPixels, ScreenPixels, ScreenPercentage } [System.Serializable] public class FloatEvent : UnityEvent {} /// The method used to find fingers to use with this component. See LeanFingerFilter documentation for more information. public LeanFingerFilter Use = new LeanFingerFilter(true); /// If there is no movement, ignore it? public bool IgnoreIfStatic { set { ignoreIfStatic = value; } get { return ignoreIfStatic; } } [FSA("IgnoreIfStatic")] [SerializeField] private bool ignoreIfStatic; /// The angle we want to detect movement along. /// 0 = Up. /// 90 = Right. /// 180 = Down. /// 270 = Left. public float Angle { set { angle = value; } get { return angle; } } [FSA("Angle")] [SerializeField] private float angle; /// Set delta to 0 if it goes negative? public bool OneWay { set { oneWay = value; } get { return oneWay; } } [FSA("OneWay")] [SerializeField] private bool oneWay; /// The coordinate space of the OnDelta values. public CoordinateType Coordinate { set { coordinate = value; } get { return coordinate; } } [FSA("Coordinate")] [SerializeField] private CoordinateType coordinate; /// The swipe delta will be multiplied by this value. public float Multiplier { set { multiplier = value; } get { return multiplier; } } [FSA("Multiplier")] [SerializeField] private float multiplier = 1.0f; /// This event is invoked when the requirements are met. /// Float = Position Delta based on your Coordinate setting. public FloatEvent OnDelta { get { if (onDelta == null) onDelta = new FloatEvent(); return onDelta; } } [SerializeField] private FloatEvent onDelta; /// 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() { // Get fingers var fingers = Use.UpdateAndGetFingers(); if (fingers.Count > 0 && onDelta != null) { var finalDelta = (Quaternion.Euler(0.0f, 0.0f, angle) * LeanGesture.GetScreenDelta(fingers)).y; switch (coordinate) { case CoordinateType.ScaledPixels: finalDelta *= LeanTouch.ScalingFactor; break; case CoordinateType.ScreenPercentage: finalDelta *= LeanTouch.ScreenFactor; break; } if (oneWay == true && finalDelta < 0.0f) { finalDelta = 0.0f; } finalDelta *= multiplier; onDelta.Invoke(finalDelta); } } } } #if UNITY_EDITOR namespace Lean.Touch.Editor { using TARGET = LeanMultiDirection; [UnityEditor.CanEditMultipleObjects] [UnityEditor.CustomEditor(typeof(TARGET))] public class LeanMultiDirection_Editor : LeanEditor { protected override void OnInspector() { TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts); Draw("Use"); Draw("ignoreIfStatic", "If there is no pinching, ignore it?"); Draw("angle", "The angle we want to detect movement along.\n\n0 = Up.\n90 = Right.\n180 = Down.\n270 = Left."); Draw("oneWay", "Set delta to 0 if it goes negative?"); Separator(); Draw("coordinate", "The coordinate space of the OnDelta values."); Draw("multiplier", "The delta will be multiplied by this value."); Separator(); Draw("onDelta"); } } } #endif