using UnityEngine; using UnityEngine.Events; using Lean.Common; using FSA = UnityEngine.Serialization.FormerlySerializedAsAttribute; namespace Lean.Touch { /// This component calls the OnFingerTap event when a finger taps the screen. [HelpURL(LeanTouch.HelpUrlPrefix + "LeanFingerTap")] [AddComponentMenu(LeanTouch.ComponentPathPrefix + "Finger Tap")] public class LeanFingerTap : MonoBehaviour { [System.Serializable] public class LeanFingerEvent : UnityEvent {} [System.Serializable] public class Vector3Event : UnityEvent {} [System.Serializable] public class Vector2Event : UnityEvent {} [System.Serializable] public class IntEvent : UnityEvent {} /// Ignore fingers with StartedOverGui? public bool IgnoreStartedOverGui { set { ignoreStartedOverGui = value; } get { return ignoreStartedOverGui; } } [FSA("IgnoreStartedOverGui")] [SerializeField] private bool ignoreStartedOverGui = true; /// Ignore fingers with OverGui? public bool IgnoreIsOverGui { set { ignoreIsOverGui = value; } get { return ignoreIsOverGui; } } [FSA("IgnoreIsOverGui")] [SerializeField] private bool ignoreIsOverGui; /// If the specified object is set and isn't selected, then this component will do nothing. public LeanSelectable RequiredSelectable { set { requiredSelectable = value; } get { return requiredSelectable; } } [FSA("RequiredSelectable")] [SerializeField] private LeanSelectable requiredSelectable; /// How many times must this finger tap before OnTap gets called? /// 0 = Every time (keep in mind OnTap will only be called once if you use this). public int RequiredTapCount { set { requiredTapCount = value; } get { return requiredTapCount; } } [FSA("RequiredTapCount")] [SerializeField] private int requiredTapCount; /// How many times repeating must this finger tap before OnTap gets called? /// 0 = Every time (e.g. a setting of 2 means OnTap will get called when you tap 2 times, 4 times, 6, 8, 10, etc). public int RequiredTapInterval { set { requiredTapInterval = value; } get { return requiredTapInterval; } } [FSA("RequiredTapInterval")] [SerializeField] private int requiredTapInterval; /// This event will be called if the above conditions are met when you tap the screen. public LeanFingerEvent OnFinger { get { if (onFinger == null) onFinger = new LeanFingerEvent(); return onFinger; } } [FSA("onTap")] [FSA("OnTap")] [SerializeField] private LeanFingerEvent onFinger; /// This event will be called if the above conditions are met when you tap the screen. /// Int = The finger tap count. public IntEvent OnCount { get { if (onCount == null) onCount = new IntEvent(); return onCount; } } [SerializeField] private IntEvent onCount; /// The method used to find world coordinates from a finger. See LeanScreenDepth documentation for more information. public LeanScreenDepth ScreenDepth = new LeanScreenDepth(LeanScreenDepth.ConversionType.DepthIntercept); /// This event will be called if the above conditions are met when you tap the screen. /// Vector3 = Finger position in world space. public Vector3Event OnWorld { get { if (onWorld == null) onWorld = new Vector3Event(); return onWorld; } } [FSA("onPosition")] [SerializeField] private Vector3Event onWorld; /// This event will be called if the above conditions are met when you tap the screen. /// Vector2 = Finger position in screen space. public Vector2Event OnScreen { get { if (onScreen == null) onScreen = new Vector2Event(); return onScreen; } } [SerializeField] private Vector2Event onScreen; #if UNITY_EDITOR protected virtual void Reset() { requiredSelectable = GetComponentInParent(); } #endif protected virtual void Start() { if (requiredSelectable == null) { requiredSelectable = GetComponentInParent(); } } protected virtual void OnEnable() { LeanTouch.OnFingerTap += HandleFingerTap; } protected virtual void OnDisable() { LeanTouch.OnFingerTap -= HandleFingerTap; } private void HandleFingerTap(LeanFinger finger) { // Ignore? if (ignoreStartedOverGui == true && finger.StartedOverGui == true) { return; } if (ignoreIsOverGui == true && finger.IsOverGui == true) { return; } if (requiredTapCount > 0 && finger.TapCount != requiredTapCount) { return; } if (requiredTapInterval > 0 && (finger.TapCount % requiredTapInterval) != 0) { return; } if (requiredSelectable != null && requiredSelectable.IsSelected == false) { return; } if (onFinger != null) { onFinger.Invoke(finger); } if (onCount != null) { onCount.Invoke(finger.TapCount); } if (onWorld != null) { var position = ScreenDepth.Convert(finger.ScreenPosition, gameObject); onWorld.Invoke(position); } if (onScreen != null) { onScreen.Invoke(finger.ScreenPosition); } } } } #if UNITY_EDITOR namespace Lean.Touch.Editor { using TARGET = LeanFingerTap; [UnityEditor.CanEditMultipleObjects] [UnityEditor.CustomEditor(typeof(TARGET))] public class LeanFingerTap_Editor : LeanEditor { protected override void OnInspector() { TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts); Draw("ignoreStartedOverGui", "Ignore fingers with StartedOverGui?"); Draw("ignoreIsOverGui", "Ignore fingers with OverGui?"); Draw("requiredSelectable", "If the specified object is set and isn't selected, then this component will do nothing."); Draw("requiredTapCount", "How many times must this finger tap before OnTap gets called?\n\n0 = Every time (keep in mind OnTap will only be called once if you use this)."); Draw("requiredTapInterval", "How many times repeating must this finger tap before OnTap gets called?\n\n0 = Every time (e.g. a setting of 2 means OnTap will get called when you tap 2 times, 4 times, 6, 8, 10, etc)."); Separator(); var showUnusedEvents = DrawFoldout("Show Unused Events", "Show all events?"); if (Any(tgts, t => t.OnFinger.GetPersistentEventCount() > 0) == true || showUnusedEvents == true) { Draw("onFinger"); } if (Any(tgts, t => t.OnCount.GetPersistentEventCount() > 0) == true || showUnusedEvents == true) { Draw("onCount"); } if (Any(tgts, t => t.OnWorld.GetPersistentEventCount() > 0) == true || showUnusedEvents == true) { Draw("ScreenDepth"); Draw("onWorld"); } if (Any(tgts, t => t.OnScreen.GetPersistentEventCount() > 0) == true || showUnusedEvents == true) { Draw("onScreen"); } } } } #endif